I've got a matrix which can be defined using a parameter, so this is like for a parameter 'a' I can label my Matrix as A(a) where the entries depend on this 'a', the thing is that I'd like to create a set of this matrices using a domain for this 'a's. So my question is, How do I define the set of 'a' in order to obtain the set of matrices?
Asked
Active
Viewed 229 times
0
-
Hi, I've got a set of values for 'a' (real values in general), and each entry in this matrix is a function of this parameter, I'd like to generate a set of this matrices for each value of a. – Armando Pezo Sep 20 '17 at 22:28
-
You have been happy of obtaining answers on SO? Consider reading [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers). – keepAlive Oct 29 '17 at 20:30
1 Answers
0
Something you can do is using the method fromfunction
of the numpy
library. Then with a (nxn) square matrix it would be as simple as
import numpy as np
def your_function(row_index, col_index, a):
# ... do stuff and compute your result
return result
n = 2
a = np.pi
your_matrix = np.fromfunction(lambda i, j: your_function(i, j, a), (n, n), dtype=np.float)
and if you want it to be in a callable form, actually as you asked, you can do
A = lambda a : np.fromfunction(lambda i, j: your_function(i, j, a), (n, n), dtype=np.float)
Or if you do not want to use an anonymous function
def A(a):
return np.fromfunction(lambda i, j: your_function(i, j, a), (n, n), dtype=np.float)
Let do a simple example
def your_function(row_index, col_index, a):
return row_index + col_index + a
n = 2
A = lambda a : np.fromfunction(lambda i, j: your_function(i, j, a), (n, n), dtype=np.float)
Use case
>>> A(1)
array([[ 1., 2.],
[ 2., 3.]])
>>> A(np.pi)
array([[ 3.1415926535897931, 4.1415926535897931],
[ 4.1415926535897931, 5.1415926535897931]])

keepAlive
- 6,369
- 5
- 24
- 39
-
If I answered your question, the way this site works, you'd "accept" the answer, more here: [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – keepAlive Oct 08 '17 at 03:15