Skip to content

Commit 79216c4

Browse files
Chaluvadisyurkevi
authored andcommitted
added linear algebra examples
1 parent 701fa91 commit 79216c4

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

examples/lin_algebra/cholesky.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python
2+
#######################################################
3+
# Copyright (c) 2024, ArrayFire
4+
# All rights reserved.
5+
#
6+
# This file is distributed under 3-clause BSD license.
7+
# The complete license agreement can be obtained at:
8+
# http://arrayfire.com/licenses/BSD-3-Clause
9+
########################################################
10+
11+
import arrayfire as af
12+
13+
14+
def main():
15+
try:
16+
af.info()
17+
18+
n = 5
19+
t = af.randu((n, n))
20+
arr_in = af.matmul(t, t, rhs_opts=af.MatProp.TRANS) + af.identity((n, n)) * n
21+
22+
print("Running Cholesky InPlace\n")
23+
cin_upper = arr_in.copy()
24+
cin_lower = arr_in.copy()
25+
26+
af.cholesky(cin_upper, True)
27+
af.cholesky(cin_lower, False)
28+
29+
print(cin_upper)
30+
print(cin_lower)
31+
32+
print("\nRunning Cholesky Out of place\n")
33+
34+
out_upper, upper_success = af.cholesky(arr_in, True)
35+
out_lower, lower_success = af.cholesky(arr_in, False)
36+
37+
if upper_success == 0:
38+
print(out_upper)
39+
if lower_success == 0:
40+
print(out_lower)
41+
42+
except Exception as e:
43+
print("Error: ", str(e))
44+
45+
46+
if __name__ == "__main__":
47+
main()

examples/lin_algebra/lu.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python
2+
#######################################################
3+
# Copyright (c) 2024, ArrayFire
4+
# All rights reserved.
5+
#
6+
# This file is distributed under 3-clause BSD license.
7+
# The complete license agreement can be obtained at:
8+
# http://arrayfire.com/licenses/BSD-3-Clause
9+
########################################################
10+
11+
import arrayfire as af
12+
13+
14+
def main():
15+
try:
16+
af.info()
17+
18+
in_array = af.randu((5, 8))
19+
20+
print("Running LU InPlace\n")
21+
pivot = af.lu(in_array, inplace=True)
22+
print(in_array)
23+
print(pivot)
24+
25+
print("Running LU with Upper Lower Factorization\n")
26+
lower, upper, pivot = af.lu(in_array)
27+
print(lower)
28+
print(upper)
29+
print(pivot)
30+
31+
except Exception as e:
32+
print("Error: ", str(e))
33+
34+
35+
if __name__ == "__main__":
36+
main()

examples/lin_algebra/qr.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python
2+
#######################################################
3+
# Copyright (c) 2024, ArrayFire
4+
# All rights reserved.
5+
#
6+
# This file is distributed under 3-clause BSD license.
7+
# The complete license agreement can be obtained at:
8+
# http://arrayfire.com/licenses/BSD-3-Clause
9+
########################################################
10+
11+
import arrayfire as af
12+
13+
14+
def main():
15+
try:
16+
af.info()
17+
in_array = af.randu((5, 8))
18+
19+
print("Running QR InPlace\n")
20+
q_in = in_array.copy()
21+
print(q_in)
22+
23+
tau = af.qr(q_in, inplace=True)
24+
25+
print(q_in)
26+
print(tau)
27+
28+
print("Running QR with Q and R factorization\n")
29+
q, r, tau = af.qr(in_array)
30+
31+
print(q)
32+
print(r)
33+
print(tau)
34+
35+
except Exception as e:
36+
print("Error: ", str(e))
37+
38+
39+
if __name__ == "__main__":
40+
main()

0 commit comments

Comments
 (0)