Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nowadays math.sqrt() is fast than ** 0.5 #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions parsimony/datasets/simulate/grad.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"""
from six import with_metaclass
import abc
import math

import numpy as np

Expand Down Expand Up @@ -333,7 +334,7 @@ def project(self, alpha):
anorm = ax ** 2 + ay ** 2 + az ** 2
i = anorm > 1.0

anorm_i = anorm[i] ** 0.5 # Square root is taken here. Faster.
anorm_i = math.sqrt(anorm[i])
ax[i] = np.divide(ax[i], anorm_i)
ay[i] = np.divide(ay[i], anorm_i)
az[i] = np.divide(az[i], anorm_i)
Expand Down Expand Up @@ -399,7 +400,7 @@ def project(self, a):
anorm = ax ** 2 + ay ** 2 + az ** 2
i = anorm > 1.0

anorm_i = anorm[i] ** 0.5 # Square root is taken here. Faster.
anorm_i = math.sqrt(anorm[i])
ax[i] = np.divide(ax[i], anorm_i)
ay[i] = np.divide(ay[i], anorm_i)
az[i] = np.divide(az[i], anorm_i)
Expand Down Expand Up @@ -429,7 +430,7 @@ def _Nesterov_GroupTV_project(a):
anorm = ax ** 2 + ay ** 2 + az ** 2
i = anorm > 1.0

anorm_i = anorm[i] ** 0.5 # Square root is taken here. Faster.
anorm_i = math.sqrt(anorm[i])
ax[i] = np.divide(ax[i], anorm_i)
ay[i] = np.divide(ay[i], anorm_i)
az[i] = np.divide(az[i], anorm_i)
Expand Down Expand Up @@ -495,7 +496,7 @@ def _Nesterov_TV_project(alpha):
anorm = ax ** 2 + ay ** 2 + az ** 2
i = anorm > 1.0

anorm_i = anorm[i] ** 0.5 # Square root is taken here. Faster.
anorm_i = math.sqrt(anorm[i])
ax[i] = np.divide(ax[i], anorm_i)
ay[i] = np.divide(ay[i], anorm_i)
az[i] = np.divide(az[i], anorm_i)
Expand Down
6 changes: 4 additions & 2 deletions parsimony/functions/nesterov/grouptv.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
@email: [email protected]
@license: BSD 3-clause.
"""
import math

import scipy.sparse as sparse
import numpy as np

Expand Down Expand Up @@ -181,7 +183,7 @@ def project(self, a):
anorm = ax ** 2 + ay ** 2 + az ** 2
i = anorm > 1.0

anorm_i = anorm[i] ** 0.5 # Square root is taken here. Faster.
anorm_i = math.sqrt(anorm[i])
ax[i] = np.divide(ax[i], anorm_i)
ay[i] = np.divide(ay[i], anorm_i)
az[i] = np.divide(az[i], anorm_i)
Expand Down Expand Up @@ -225,7 +227,7 @@ def estimate_mu(self, beta):
ay = A[g + 1].dot(beta_)
az = A[g + 2].dot(beta_)

anorm = (ax ** 2 + ay ** 2 + az ** 2) ** 0.5 # Square root is taken here. Faster.
anorm = math.sqrt(ax ** 2 + ay ** 2 + az ** 2)

max_norm = max(max_norm, np.max(anorm)) # The overall maximum.

Expand Down
2 changes: 1 addition & 1 deletion parsimony/functions/nesterov/l1tv.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def project(self, a):
anorm_tv += a[k] ** 2
i_tv = anorm_tv > 1.0

anorm_tv_i = anorm_tv[i_tv] ** 0.5 # Square root is taken here. Faster.
anorm_tv_i = math.sqrt(anorm_tv[i_tv])
for k in range(1, len(a)):
a[k][i_tv] = np.divide(a[k][i_tv], anorm_tv_i)

Expand Down
4 changes: 2 additions & 2 deletions parsimony/functions/nesterov/tv.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def lambda_max(self):
# anorm = ax ** 2 + ay ** 2 + az ** 2
# i = anorm > 1.0
#
# anorm_i = anorm[i] ** 0.5 # Square root is taken here. Faster.
# anorm_i = math.sqrt(anorm[i])
# ax[i] = np.divide(ax[i], anorm_i)
# ay[i] = np.divide(ay[i], anorm_i)
# az[i] = np.divide(az[i], anorm_i)
Expand All @@ -261,7 +261,7 @@ def project(self, a):
anorm += a[k] ** 2
i = anorm > 1.0

anorm_i = anorm[i] ** 0.5 # Square root is taken here. Faster.
anorm_i = math.sqrt(anorm[i])
for k in range(len(a)):
a[k][i] = np.divide(a[k][i], anorm_i)

Expand Down
3 changes: 2 additions & 1 deletion parsimony/functions/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
@license: BSD 3-clause.
"""
import abc
import math
from six import with_metaclass

import numpy as np
Expand Down Expand Up @@ -1042,7 +1043,7 @@ def Ata(self, a):
# anorm = ax ** 2 + ay ** 2 + az ** 2
# i = anorm > 1.0
#
# anorm_i = anorm[i] ** 0.5 # Square root is taken here. Faster.
# anorm_i = math.sqrt(anorm[i])
# ax[i] = np.divide(ax[i], anorm_i)
# ay[i] = np.divide(ay[i], anorm_i)
# az[i] = np.divide(az[i], anorm_i)
Expand Down