svdvals#
- ivy.svdvals(x, /, *, driver=None, out=None)[source]#
Return the singular values of a matrix (or a stack of matrices)
x
.- Parameters:
x (
Union
[Array
,NativeArray
]) – input array having shape(..., M, N)
and whose innermost two dimensions formMxN
matrices.driver (
Optional
[str
], default:None
) – optional output array,name of the cuSOLVER method to be used. This keyword argument only works on CUDA inputs. Available options are: None, gesvd, gesvdj, and gesvda.Default: None.out (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
- Returns:
ret – array with shape
(..., K)
that contains the vector(s) of singular values of lengthK
, where K = min(M, N). The values are sorted in descending order by magnitude. The returned array must have a real-valued floating-point data type having the same precision asx
(e.g., ifx
iscomplex64
, the returned array must have afloat32
data type).
This function conforms to the Array API Standard. This docstring is an extension of the docstring in the standard.
Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts
ivy.Container
instances in place of any of the arguments.Examples
With
ivy.Array
input:>>> x = ivy.array([[5.0, 7.0], [4.0, 3.0]]) >>> y = ivy.svdvals(x) >>> print(y.shape) ivy.Shape(2,)
With comparison of the singular value S ivy.svdvals() by the result ivy.svd().
>>> x = ivy.array([[5.0, 7.0], [4.0, 3.0]]) >>> _, y, _ = ivy.svd(x) >>> print(y.shape) ivy.Shape(2,)
>>> x = ivy.array([9.86217213, 1.31816804]) >>> y = ivy.array([9.86217213, 1.31816804]) >>> error = (x - y).abs() >>> print(error) ivy.array([0.,0.])
With
ivy.NativeArray
input:>>> x = ivy.native_array([[1.0, 2.0, 3.0], [2.0, 3.0, 4.0], ... [2.0, 1.0, 3.0], [3.0, 4.0, 5.0]]) >>> x.shape (4, 3)
>>> x = ivy.native_array([[1.0, 2.0, 3.0], [2.0, 3.0, 4.0], ... [2.0, 1.0, 3.0], [3.0, 4.0, 5.0]]) >>> y = ivy.svdvals(x) >>> print(y) ivy.array([10.3, 1.16, 0.615])
>>> _, SS, _ = ivy.svd(x) >>> print(SS) ivy.array([10.3, 1.16, 0.615])
with comparison of singular value S ivy.svdvals() by the result ivy.svd().
>>> x = ivy.array([10.25994301, 1.16403675, 0.61529762]) >>> y = ivy.array([9.86217213, 1.31816804, 0.51231241]) >>> error = (x - y).abs() >>> print(error) ivy.array([0.39777088, 0.15413129, 0.1029852 ])
With
ivy.Container
input:>>> x = ivy.Container(a=ivy.array([[2.0, 3.0], [3.0, 4.0], ... [1.0, 3.0], [3.0, 5.0]]), ... b=ivy.array([[7.0, 1.0, 2.0, 3.0], ... [2.0, 5.0, 3.0, 4.0], ... [2.0, 6.0, 1.0, 3.0], ... [3.0, 4.0, 5.0, 9.0]])) >>> y = ivy.svdvals(x) >>> print(y) { a: ivy.array([9.01383495, 0.86647356]), b: ivy.array([15.7786541, 5.55970621, 4.16857576, 0.86412698]) }
Instance Method Examples
Using
ivy.Array
instance method:>>> x = ivy.array([[8.0, 3.0], [2.0, 3.0], ... [2.0, 1.0], [3.0, 4.0], ... [4.0, 1.0], [5.0, 6.0]]) >>> y = x.svdvals() >>> print(y) ivy.array([13.37566757, 3.88477993])
With
ivy.Container
instance method:>>> x = ivy.Container(a=ivy.array([[2.0, 3.0, 6.0], [5.0, 3.0, 4.0], ... [1.0, 7.0, 3.0], [3.0, 2.0, 5.0]]), ... b=ivy.array([[7.0, 1.0, 2.0, 3.0, 9.0], ... [2.0, 5.0, 3.0, 4.0, 10.0], ... [2.0, 11.0, 6.0, 1.0, 3.0], ... [8.0, 3.0, 4.0, 5.0, 9.0]])) >>> y = x.svdvals() >>> print(y) { a: ivy.array([12.95925522, 4.6444726, 2.54687881]), b: ivy.array([23.16134834, 10.35037804, 4.31025076, 1.35769391]) }