/usr/lib/python2.7/dist-packages/arrayfire/lapack.py is in python-arrayfire 3.3.20160624-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | #######################################################
# Copyright (c) 2015, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################
"""
Dense Linear Algebra functions (solve, inverse, etc).
"""
from .library import *
from .array import *
def lu(A):
"""
LU decomposition.
Parameters
----------
A: af.Array
A 2 dimensional arrayfire array.
Returns
-------
(L,U,P): tuple of af.Arrays
- L - Lower triangular matrix.
- U - Upper triangular matrix.
- P - Permutation array.
Note
----
The original matrix `A` can be reconstructed using the outputs in the following manner.
>>> A[P, :] = af.matmul(L, U)
"""
L = Array()
U = Array()
P = Array()
safe_call(backend.get().af_lu(ct.pointer(L.arr), ct.pointer(U.arr), ct.pointer(P.arr), A.arr))
return L,U,P
def lu_inplace(A, pivot="lapack"):
"""
In place LU decomposition.
Parameters
----------
A: af.Array
- a 2 dimensional arrayfire array on entry.
- Contains L in the lower triangle on exit.
- Contains U in the upper triangle on exit.
Returns
-------
P: af.Array
- Permutation array.
Note
----
This function is primarily used with `af.solve_lu` to reduce computations.
"""
P = Array()
is_pivot_lapack = False if (pivot == "full") else True
safe_call(backend.get().af_lu_inplace(ct.pointer(P.arr), A.arr, is_pivot_lapack))
return P
def qr(A):
"""
QR decomposition.
Parameters
----------
A: af.Array
A 2 dimensional arrayfire array.
Returns
-------
(Q,R,T): tuple of af.Arrays
- Q - Orthogonal matrix.
- R - Upper triangular matrix.
- T - Vector containing additional information to solve a least squares problem.
Note
----
The outputs of this funciton have the following properties.
>>> A = af.matmul(Q, R)
>>> I = af.matmulNT(Q, Q) # Identity matrix
"""
Q = Array()
R = Array()
T = Array()
safe_call(backend.get().af_lu(ct.pointer(Q.arr), ct.pointer(R.arr), ct.pointer(T.arr), A.arr))
return Q,R,T
def qr_inplace(A):
"""
In place QR decomposition.
Parameters
----------
A: af.Array
- a 2 dimensional arrayfire array on entry.
- Packed Q and R matrices on exit.
Returns
-------
T: af.Array
- Vector containing additional information to solve a least squares problem.
Note
----
This function is used to save space only when `R` is required.
"""
T = Array()
safe_call(backend.get().af_qr_inplace(ct.pointer(T.arr), A.arr))
return T
def cholesky(A, is_upper=True):
"""
Cholesky decomposition
Parameters
----------
A: af.Array
A 2 dimensional, symmetric, positive definite matrix.
is_upper: optional: bool. default: True
Specifies if output `R` is upper triangular (if True) or lower triangular (if False).
Returns
-------
(R,info): tuple of af.Array, int.
- R - triangular matrix.
- info - 0 if decomposition sucessful.
Note
----
The original matrix `A` can be reconstructed using the outputs in the following manner.
>>> A = af.matmulNT(R, R) #if R is upper triangular
"""
R = Array()
info = ct.c_int(0)
safe_call(backend.get().af_cholesky(ct.pointer(R.arr), ct.pointer(info), A.arr, is_upper))
return R, info.value
def cholesky_inplace(A, is_upper=True):
"""
In place Cholesky decomposition.
Parameters
----------
A: af.Array
- a 2 dimensional, symmetric, positive definite matrix.
- Trinangular matrix on exit.
is_upper: optional: bool. default: True.
Specifies if output `R` is upper triangular (if True) or lower triangular (if False).
Returns
-------
info : int.
0 if decomposition sucessful.
"""
info = ct.c_int(0)
safe_call(backend.get().af_cholesky_inplace(ct.pointer(info), A.arr, is_upper))
return info.value
def solve(A, B, options=MATPROP.NONE):
"""
Solve a system of linear equations.
Parameters
----------
A: af.Array
A 2 dimensional arrayfire array representing the coefficients of the system.
B: af.Array
A 1 or 2 dimensional arrayfire array representing the constants of the system.
options: optional: af.MATPROP. default: af.MATPROP.NONE.
- Additional options to speed up computations.
- Currently needs to be one of `af.MATPROP.NONE`, `af.MATPROP.LOWER`, `af.MATPROP.UPPER`.
Returns
-------
X: af.Array
A 1 or 2 dimensional arrayfire array representing the unknowns in the system.
"""
X = Array()
safe_call(backend.get().af_solve(ct.pointer(X.arr), A.arr, B.arr, options.value))
return X
def solve_lu(A, P, B, options=MATPROP.NONE):
"""
Solve a system of linear equations, using LU decomposition.
Parameters
----------
A: af.Array
- A 2 dimensional arrayfire array representing the coefficients of the system.
- This matrix should be decomposed previously using `lu_inplace(A)`.
P: af.Array
- Permutation array.
- This array is the output of an earlier call to `lu_inplace(A)`
B: af.Array
A 1 or 2 dimensional arrayfire array representing the constants of the system.
Returns
-------
X: af.Array
A 1 or 2 dimensional arrayfire array representing the unknowns in the system.
"""
X = Array()
safe_call(backend.get().af_solve_lu(ct.pointer(X.arr), A.arr, P.arr, B.arr, options.value))
return X
def inverse(A, options=MATPROP.NONE):
"""
Invert a matrix.
Parameters
----------
A: af.Array
- A 2 dimensional arrayfire array
options: optional: af.MATPROP. default: af.MATPROP.NONE.
- Additional options to speed up computations.
- Currently needs to be one of `af.MATPROP.NONE`.
Returns
-------
AI: af.Array
- A 2 dimensional array that is the inverse of `A`
Note
----
`A` needs to be a square matrix.
"""
AI = Array()
safe_call(backend.get().af_inverse(ct.pointer(AI.arr), A.arr, options.value))
return AI
def rank(A, tol=1E-5):
"""
Rank of a matrix.
Parameters
----------
A: af.Array
- A 2 dimensional arrayfire array
tol: optional: scalar. default: 1E-5.
- Tolerance for calculating rank
Returns
-------
r: int
- Rank of `A` within the given tolerance
"""
r = ct.c_uint(0)
safe_call(backend.get().af_rank(ct.pointer(r), A.arr, ct.c_double(tol)))
return r.value
def det(A):
"""
Determinant of a matrix.
Parameters
----------
A: af.Array
- A 2 dimensional arrayfire array
Returns
-------
res: scalar
- Determinant of the matrix.
"""
re = ct.c_double(0)
im = ct.c_double(0)
safe_call(backend.get().af_det(ct.pointer(re), ct.pointer(im), A.arr))
re = re.value
im = im.value
return re if (im == 0) else re + im * 1j
def norm(A, norm_type=NORM.EUCLID, p=1.0, q=1.0):
"""
Norm of an array or a matrix.
Parameters
----------
A: af.Array
- A 1 or 2 dimensional arrayfire array
norm_type: optional: af.NORM. default: af.NORM.EUCLID.
- Type of norm to be calculated.
p: scalar. default 1.0.
- Used only if `norm_type` is one of `af.NORM.VECTOR_P`, `af.NORM_MATRIX_L_PQ`
q: scalar. default 1.0.
- Used only if `norm_type` is `af.NORM_MATRIX_L_PQ`
Returns
-------
res: scalar
- norm of the input
"""
res = ct.c_double(0)
safe_call(backend.get().af_norm(ct.pointer(res), A.arr, norm_type.value,
ct.c_double(p), ct.c_double(q)))
return res.value
def svd(A):
"""
Singular Value Decomposition
Parameters
----------
A: af.Array
A 2 dimensional arrayfire array.
Returns
-------
(U,S,Vt): tuple of af.Arrays
- U - A unitary matrix
- S - An array containing the elements of diagonal matrix
- Vt - A unitary matrix
Note
----
- The original matrix `A` is preserved and additional storage space is required for decomposition.
- If the original matrix `A` need not be preserved, use `svd_inplace` instead.
- The original matrix `A` can be reconstructed using the outputs in the following manner.
>>> Smat = af.diag(S, 0, False)
>>> A_recon = af.matmul(af.matmul(U, Smat), Vt)
"""
U = Array()
S = Array()
Vt = Array()
safe_call(backend.get().af_svd(ct.pointer(U.arr), ct.pointer(S.arr), ct.pointer(Vt.arr), A.arr))
return U, S, Vt
def svd_inplace(A):
"""
Singular Value Decomposition
Parameters
----------
A: af.Array
A 2 dimensional arrayfire array.
Returns
-------
(U,S,Vt): tuple of af.Arrays
- U - A unitary matrix
- S - An array containing the elements of diagonal matrix
- Vt - A unitary matrix
Note
----
- The original matrix `A` is not preserved.
- If the original matrix `A` needs to be preserved, use `svd` instead.
- The original matrix `A` can be reconstructed using the outputs in the following manner.
>>> Smat = af.diag(S, 0, False)
>>> A_recon = af.matmul(af.matmul(U, Smat), Vt)
"""
U = Array()
S = Array()
Vt = Array()
safe_call(backend.get().af_svd_inplace(ct.pointer(U.arr), ct.pointer(S.arr), ct.pointer(Vt.arr),
A.arr))
return U, S, Vt
def is_lapack_available():
"""
Function to check if the arrayfire library was built with lapack support.
"""
res = ct.c_bool(False)
safe_call(backend.get().af_is_lapack_available(ct.pointer(res)))
return res.value
|