|
8 | 8 | ########################################################
|
9 | 9 |
|
10 | 10 | """
|
11 |
| -dense linear algebra functions for arrayfire. |
| 11 | +Dense Linear Algebra functions for arrayfire. |
12 | 12 | """
|
13 | 13 |
|
14 | 14 | from .library import *
|
@@ -339,3 +339,72 @@ def norm(A, norm_type=NORM.EUCLID, p=1.0, q=1.0):
|
339 | 339 | safe_call(backend.get().af_norm(ct.pointer(res), A.arr, norm_type.value,
|
340 | 340 | ct.c_double(p), ct.c_double(q)))
|
341 | 341 | return res.value
|
| 342 | + |
| 343 | +def svd(A): |
| 344 | + """ |
| 345 | + Singular Value Decomposition |
| 346 | +
|
| 347 | + Parameters |
| 348 | + ---------- |
| 349 | + A: af.Array |
| 350 | + A 2 dimensional arrayfire array. |
| 351 | +
|
| 352 | + Returns |
| 353 | + ------- |
| 354 | + (U,S,Vt): tuple of af.Arrays |
| 355 | + - U - A unitary matrix |
| 356 | + - S - An array containing the elements of diagonal matrix |
| 357 | + - Vt - A unitary matrix |
| 358 | +
|
| 359 | + Note |
| 360 | + ---- |
| 361 | +
|
| 362 | + - The original matrix `A` is preserved and additional storage space is required for decomposition. |
| 363 | +
|
| 364 | + - If the original matrix `A` need not be preserved, use `svd_inplace` instead. |
| 365 | +
|
| 366 | + - The original matrix `A` can be reconstructed using the outputs in the following manner. |
| 367 | + >>> Smat = af.diag(S, 0, False) |
| 368 | + >>> A_recon = af.matmul(af.matmul(U, Smat), Vt) |
| 369 | +
|
| 370 | + """ |
| 371 | + U = Array() |
| 372 | + S = Array() |
| 373 | + Vt = Array() |
| 374 | + safe_call(backend.get().af_svd(ct.pointer(U.arr), ct.pointer(S.arr), ct.pointer(Vt.arr), A.arr)) |
| 375 | + return U, S, Vt |
| 376 | + |
| 377 | +def svd_inplace(A): |
| 378 | + """ |
| 379 | + Singular Value Decomposition |
| 380 | +
|
| 381 | + Parameters |
| 382 | + ---------- |
| 383 | + A: af.Array |
| 384 | + A 2 dimensional arrayfire array. |
| 385 | +
|
| 386 | + Returns |
| 387 | + ------- |
| 388 | + (U,S,Vt): tuple of af.Arrays |
| 389 | + - U - A unitary matrix |
| 390 | + - S - An array containing the elements of diagonal matrix |
| 391 | + - Vt - A unitary matrix |
| 392 | +
|
| 393 | + Note |
| 394 | + ---- |
| 395 | +
|
| 396 | + - The original matrix `A` is not preserved. |
| 397 | +
|
| 398 | + - If the original matrix `A` needs to be preserved, use `svd` instead. |
| 399 | +
|
| 400 | + - The original matrix `A` can be reconstructed using the outputs in the following manner. |
| 401 | + >>> Smat = af.diag(S, 0, False) |
| 402 | + >>> A_recon = af.matmul(af.matmul(U, Smat), Vt) |
| 403 | +
|
| 404 | + """ |
| 405 | + U = Array() |
| 406 | + S = Array() |
| 407 | + Vt = Array() |
| 408 | + safe_call(backend.get().af_svd_inplace(ct.pointer(U.arr), ct.pointer(S.arr), ct.pointer(Vt.arr), |
| 409 | + A.arr)) |
| 410 | + return U, S, Vt |
0 commit comments