Closed fedeinthemix closed 7 years ago
I agree. The name is a bad translation, and the function should be more general. I think the following definition works for Double, Float, Complex Double, and Complex Float elements:
normalize v = v / scalar (sqrt (sumElements (conj v * v)))
I will include this change in the next release. Thanks!
I was thinking that it is a pity not to be able to use the function 'norm_2' for this purpose. I suppose the problem is that the various norm functions return a 'Double'.
Most of the time the norm of a vector is used to scale a vector or some product of vectors. To allow composability of the vairous 'norm_' functions with other vector operations, wouldn't it be useful for norms, to return a value of the same type as the elements of the vector to which it is applied (for Double, Float, Complex Double, Complex Float)?
Sounds great. Thanks you!
The problem of returning a complex value is that for, example, we cannot directly compare the norm of a vector to a small number to check for convergence in a given process. A norm must return a real.
To get a general definition we can use real
:
λ> real 3 :: Vector Double
fromList [3.0]
λ> real 3 :: Vector (Complex Double)
fromList [3.0 :+ 0.0]
λ> let v = fromList[1,2,3,iC]
λ> v
fromList [1.0 :+ 0.0,2.0 :+ 0.0,3.0 :+ 0.0,0.0 :+ 1.0]
λ> v / real (scalar (norm_2 v))
fromList [0.2581988897471611 :+ 0.0,0.5163977794943222 :+ 0.0,0.7745966692414833 :+ 0.0,0.0 :+ 0.2581988897471611]
But it only works for Vector Double and Vector (Complex Double). We would need another adaptor for Float and Double...
I don't know which is the best decision about this.
I see. Now I understand the purpose of 'real' :-) Looking in the documentation and seeing it next to 'double', 'single' and 'complex', but with a different signature, I didn't quite understand its role.
Thanks!
The function 'unitary' is defined to only work with vectors of type 'Vector Double'. I would find it useful to generalize it to work with vectors of type 'Vector (Complex Double)' and possibly the other vector types.
Also, in linear algebra the term unitary is commonly attached to matrices with the property 'adjoint U' = 'inverse U'. I find its use to name a function used to normalize vectors a little bit strange. Maybe we could find a more appropriate name? Maybe 'normalize'?