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

LinearAlgebra.normalize(x) should work for scalars #44835

Closed
stevengj opened this issue Apr 2, 2022 · 5 comments · Fixed by #44925
Closed

LinearAlgebra.normalize(x) should work for scalars #44835

stevengj opened this issue Apr 2, 2022 · 5 comments · Fixed by #44925
Labels
good first issue Indicates a good issue for first-time contributors to Julia linear algebra Linear algebra

Comments

@stevengj
Copy link
Member

stevengj commented Apr 2, 2022

Currently, this is defined only for arrays, but it seems like we should have

normalize(x::Number) = x / abs(x)

(I just came across this when trying to implement this algorithm for random unitary matrices, which is basically Q * Diagonal(normalize.(diag(R)), but I discovered that normalize didn't work.)

@stevengj stevengj added the linear algebra Linear algebra label Apr 2, 2022
@Seelengrab
Copy link
Contributor

Is this the same as sign(x) for numbers?

@oscardssmith
Copy link
Member

it's very similar, but has different behavior for zero.

@Seelengrab
Copy link
Contributor

Does that matter for this case? If I'm reading the paper right, R should be invertible, so no zeros on the main diagonal 🤔

@stevengj
Copy link
Member Author

stevengj commented Apr 2, 2022

Yes, in this particular case, you're right that I could have used sign.

However, it still doesn't make sense to me that the normalize function isn't defined for scalars; it seems like it should be defined for any normed vector space.

Indeed, we could just define the fallback:

normalize(x) = x / norm(x)
normalize(x, p) = x / norm(x, p)

(where norm falls back to abs for scalars).

@stevengj stevengj added the good first issue Indicates a good issue for first-time contributors to Julia label Apr 2, 2022
@mikerouleau
Copy link
Contributor

I've been meaning to get more involved and this looks like a good first issue. I should have a PR ready later today.

mikerouleau added a commit to mikerouleau/julia that referenced this issue Apr 9, 2022
mikerouleau added a commit to mikerouleau/julia that referenced this issue Apr 9, 2022
dkarrasch pushed a commit that referenced this issue Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Indicates a good issue for first-time contributors to Julia linear algebra Linear algebra
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants