-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
191 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
from pyccel.decorators import pure | ||
from numpy import shape | ||
|
||
@pure | ||
def matmul(a: 'float[:,:]', b: 'float[:,:]', c: 'float[:,:]'): | ||
""" | ||
Performs the matrix-matrix product a*b and writes the result into c. | ||
Parameters | ||
---------- | ||
a : array[float] | ||
The first input array (matrix). | ||
b : array[float] | ||
The second input array (matrix). | ||
c : array[float] | ||
The output array (matrix) which is the result of the matrix-matrix product a.dot(b). | ||
""" | ||
|
||
c[:, :] = 0. | ||
|
||
sh_a = shape(a) | ||
sh_b = shape(b) | ||
|
||
for i in range(sh_a[0]): | ||
for j in range(sh_b[1]): | ||
for k in range(sh_a[1]): | ||
c[i, j] += a[i, k] * b[k, j] | ||
|
||
|
||
@pure | ||
def sum_vec(a: 'float[:]') -> float: | ||
""" | ||
Sum the elements of a 1D vector. | ||
Parameters | ||
---------- | ||
a : array[float] | ||
The 1d vector. | ||
""" | ||
|
||
out = 0. | ||
|
||
sh_a = shape(a) | ||
|
||
for i in range(sh_a[0]): | ||
out += a[i] | ||
|
||
return out | ||
|
||
|
||
@pure | ||
def min_vec(a: 'float[:]') -> float: | ||
""" | ||
Compute the minimum a 1D vector. | ||
Parameters | ||
---------- | ||
a : array[float] | ||
The 1D vector. | ||
""" | ||
|
||
out = a[0] | ||
|
||
sh_a = shape(a) | ||
|
||
for i in range(sh_a[0]): | ||
if a[i] < out: | ||
out = a[i] | ||
|
||
return out | ||
|
||
|
||
@pure | ||
def max_vec(a: 'float[:]') -> float: | ||
""" | ||
Compute the maximum a 1D vector. | ||
Parameters | ||
---------- | ||
a : array[float] | ||
The 1D vector. | ||
""" | ||
|
||
out = a[0] | ||
|
||
sh_a = shape(a) | ||
|
||
for i in range(sh_a[0]): | ||
if a[i] > out: | ||
out = a[i] | ||
|
||
return out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from psydac.core.arrays import matmul, sum_vec, min_vec, max_vec | ||
|
||
@pytest.mark.parametrize('seed', [123, 6, 49573]) | ||
def test_arrays(seed): | ||
|
||
np.random.seed(seed) | ||
|
||
a = np.random.rand(30, 20) | ||
b = np.random.rand(20, 40) | ||
c = np.zeros((30, 40)) | ||
|
||
matmul(a, b, c) | ||
assert np.allclose(np.matmul(a, b), c) | ||
|
||
v = np.random.rand(32) | ||
assert np.isclose(np.sum(v), sum_vec(v)) | ||
assert np.isclose(np.min(v), min_vec(v)) | ||
assert np.isclose(np.max(v), max_vec(v)) | ||
|
||
|
||
if __name__ == '__main__': | ||
test_arrays(459) |
Oops, something went wrong.
9a016b1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely needed in Struphy. Martin wrote an issue about it, since Psydac also at some point wants to enable C compilation.