I have an equation that has a symbolic variable named a by using Sympy package. using this variable I created a 6 * 6 matrix:
array([[1, exp(-0.04*a**2), exp(-0.16*a**2), exp(-0.36*a**2),
exp(-0.64*a**2), exp(-1.0*a**2)],
[exp(-0.04*a**2), 1, exp(-0.04*a**2), exp(-0.16*a**2),
exp(-0.36*a**2), exp(-0.64*a**2)],
[exp(-0.16*a**2), exp(-0.04*a**2), 1, exp(-0.04*a**2),
exp(-0.16*a**2), exp(-0.36*a**2)],
[exp(-0.36*a**2), exp(-0.16*a**2), exp(-0.04*a**2), 1,
exp(-0.04*a**2), exp(-0.16*a**2)],
[exp(-0.64*a**2), exp(-0.36*a**2), exp(-0.16*a**2),
exp(-0.04*a**2), 1, exp(-0.04*a**2)],
[exp(-1.0*a**2), exp(-0.64*a**2), exp(-0.36*a**2),
exp(-0.16*a**2), exp(-0.04*a**2), 1]], dtype=object)
the a variable above is a sympy symbol. Now I want to inverse this matrix. Using numpy I get the following error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-117-3c958037db81> in <module>()
----> 1 inv(np.array(final_result))
D:\datascience\lib\site-packages\numpy\linalg\linalg.py in inv(a)
526 signature = 'D->D' if isComplexType(t) else 'd->d'
527 extobj = get_linalg_error_extobj(_raise_linalgerror_singular)
--> 528 ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj)
529 return wrap(ainv.astype(result_t, copy=False))
530
TypeError: No loop matching the specified signature and casting
was found for ufunc inv
and using the Sympy own syntax to inverse this matrix, which is M ** -1, takes too long and doesn't compute the results.
Is there a way to inverse this matrix, using numpy? or pure python syntax?
If you are looking for a symbolic inverse you will need to use SymPy. NumPy only computes numeric inverses, and the algorithms used by it will not work on symbolic matrices.
It looks like the default algorithm used by SymPy is slow with this matrix, but you can try alternate algorithms by passing the method keyword to the inv method of Matrix. The available options are 'GE' (the default), 'LU', and 'ADJ'.
It can also help to do M.subs(exp(-a**2/25), b) for the calculation. That way the resulting matrix is only powers of b (you will need to use rational numbers instead of floats for this to work). I was able to get an inverse with
a, b = symbols('a b')
M = Matrix([[1, exp(-a**2/25), exp(-4*a**2/25), exp(-9*a**2/25), exp(-16*a**2/25), exp(-a**2)], [exp(-a**2/25), 1, exp(-a**2/25), exp(-4*a**2/25), exp(-9*a**2/25), exp(-16*a**2/25)], [exp(-4*a**2/25), exp(-a**2/25), 1, exp(-a**2/25), exp(-4*a**2/25), exp(-9*a**2/25)], [exp(-9*a**2/25), exp(-4*a**2/25), exp(-a**2/25), 1, exp(-a**2/25), exp(-4*a**2/25)], [exp(-16*a**2/25), exp(-9*a**2/25), exp(-4*a**2/25), exp(-a**2/25), 1, exp(-a**2/25)], [exp(-a**2), exp(-16*a**2/25), exp(-9*a**2/25), exp(-4*a**2/25), exp(-a**2/25), 1]])
M2 = M.subs(exp(-a**2/25), b)
M2inv = simplify(M2.inv('ADJ')) # takes a little time to compute, about 1:30 on my machine
Minv = M2inv.subs(b, exp(-a**2/25))
At the end of the day, you may want to consider if you need the symbolic inverse of the matrix. Is it enough to substitute a numeric value for a then compute the numeric inverse with NumPy? Do you actually need the inverse at all? Usually computing the explicit inverse of a matrix is not a good idea for numeric calculations. It is generally better to use something like numpy.linalg.solve.
Symbolic inverses are in general very complicated expressions and inherently hard to compute. Numeric inverses are extremely numerically unstable and are almost never a good idea to compute to solve whatever problem you are trying to solve.
NumPy is a numerical package, and can not work with SymPy symbols.
If I understand correctly, you are using a as a SymPy symbol without assigning it a concrete value. This makes calculating an inverse very hard. For some assignments to a the matrix might even be not invertible. I am afraid SymPy's matrix inversion is not up to such task. Something that might help somewhat, is expressing the numbers as fractions, because reals are difficult to work with symbolically.
If you try out the same with smaller matrices, you'll notice that the result quickly becomes a complicated mess of symbols. It would probably be easier to numerically invert the matrix (with NumPy) for each of the values of a you need.
Another trick to try, is introducing an auxiliary symbol to obtain simpler expressions and afterwards substituting back. E.g. first something like M.subs({a**2:-log(b)}). The obtained inverse matrix might be easier to simplify. Afterwards M.subs({b: exp(-a**2)}) to investigate what happens with the original formulation. Just calculating the determinant of your original matrix with this substitution results in an expression of more than 1000 characters long, that doesn't look to be easy to simplify. There remains little hope that the complete inverse matrix would be a manageable expression.
Related
I am trying to optimise my computation for the eigenvalue/vectors of a tridiagonal matrix. My code currently takes about 30s to run and I need it to run much faster, at around 1s. I am currently using the scipy.sparse.linalg.eigsh method for finding the eigenvalue/vectors but I believe that there should be other ways which are much faster. I will post my code below so it is a little easier to see what I am trying to do. Any suggestions are greatly welcome and if there is anything unclear please let me know.
H = (dx**-2)*diags([-1, 2, -1], [-1, 0, 1], shape=(N, N))
H = sp.lil_matrix(H)
H[0,0]=0.5*H[0,0]
H[N-1,N-1]=0.5*H[N-1,N-1]
lam, phi = eigsh(H, 400, which="SM")
You're unlikely to find a simple way to speed up this calculation by 30x. As you can see from the timings in this post, even Google's impressively optimized jax library only manages a bit less than a 3x speedup over the base SVD implementation in scipy. Since SVD is equivalent to eigsh for symmetric matrices, we can expect that the speedup for eigsh will be about the same at best.
So even Google can't speed this calculation up that much, at least in the traditional way.
However, for very large, sparse matrices, there are specialized stochastic algorithms that can speed things up by much larger factors under the right circumstances. One of them has an sklearn implementation: sklearn.utils.extmath.randomized_svd. (I believe it's based on the algorithm described here, and it may also be the same as sklearn.decomposition.TruncatedSVD when algorithm='randomized'.)
Here's a slightly modified version of your example, showing how to use it:
from scipy.sparse import diags, lil_matrix
from scipy.sparse.linalg import eigsh
from sklearn.utils.extmath import randomized_svd
N = 3200
k = 400
dx = 1.0
H = (dx**-2)*diags([-1, 2, -1], [-1, 0, 1], shape=(N, N))
H = lil_matrix(H)
H[0, 0] = 0.5*H[0, 0]
H[N-1, N-1] = 0.5*H[N-1, N-1]
# lam, phi = eigsh(H, k, which="SM")
U, s, V = randomized_svd(H, k)
Here, s contains the singular values (equivalent here to eigenvalues), and U and V contain the singular vectors (equivalent here to eigenvectors). In theory, if H is a symmetric matrix, U should be the same as V.T. I didn't find that to be the case for this example, which puzzles me a bit... but I'm going ahead and posting this anyway, because this does in fact run in about a second, and so might be a solution for you.
However, there is still one more big caveat. You're passing which="SM" to eigsh, which as I understand it means you are asking for the 400 smallest eigenvalues. Is that really what you want? In almost all applications I am aware of, you'd want the 400 largest eigenvalues. If in fact you really do want the 400 smallest eigenvalues, then this won't work, because it relies on the fact that the largest eigenvalues are easier to coax out using random matrix projections.
The upshot is, you will want to test this to see if it actually gives results you can use. But it's an interesting possible solution, given what you've said so far about your problem.
I need to solve a set of simultaneous equations of the form Ax = B for x. I've used the numpy.linalg.solve function, inputting A and B, but I get the error 'LinAlgError: Last 2 dimensions of the array must be square'. How do I fix this?
Here's my code:
A = matrix([[v1x, v2x], [v1y, v2y], [v1z, v2z]])
print A
B = [(p2x-p1x-nmag[0]), (p2y-p1y-nmag[1]), (p2z-p1z-nmag[2])]
print B
x = numpy.linalg.solve(A, B)
The values of the matrix/vector are calculated earlier in the code and this works fine, but the values are:
A =
(-0.56666301, -0.52472909)
(0.44034147, 0.46768087)
(0.69641397, 0.71129036)
B =
(-0.38038602567630364, -24.092279373295057, 0.0)
x should have the form (x1,x2,0)
In case you still haven't found an answer, or in case someone in the future has this question.
To solve Ax=b:
numpy.linalg.solve uses LAPACK gesv. As mentioned in the documentation of LAPACK, gesv requires A to be square:
LA_GESV computes the solution to a real or complex linear system of equations AX = B, where A is a square matrix and X and B are rectangular matrices or vectors. Gaussian elimination with row interchanges is used to factor A as A = PL*U , where P is a permutation matrix, L is unit lower triangular, and U is upper triangular. The factored form of A is then used to solve the above system.
If A matrix is not square, it means that you either have more variables than your equations or the other way around. In these situations, you can have the cases of no solution or infinite number of solutions. What determines the solution space is the rank of the matrix compared to the number of columns. Therefore, you first have to check the rank of the matrix.
That being said, you can use another method to solve your system of linear equations. I suggest having a look at factorization methods like LU or QR or even SVD. In LAPACK you can use getrs, in Python you can different things:
first do the factorization like QR and then feed the resulting matrices to a method like scipy.linalg.solve_triangular
solve the least-squares using numpy.linalg.lstsq
Also have a look here where a simple example is formulated and solved.
A square matrix is a matrix with the same number of rows and columns. The matrix you are doing is a 3 by 2. Add a column of zeroes to fix this problem.
Using example from Andrew Ng's class (finding parameters for Linear Regression using normal equation):
With Python:
X = np.array([[1, 2104, 5, 1, 45], [1, 1416, 3, 2, 40], [1, 1534, 3, 2, 30], [1, 852, 2, 1, 36]])
y = np.array([[460], [232], [315], [178]])
θ = ((np.linalg.inv(X.T.dot(X))).dot(X.T)).dot(y)
print(θ)
Result:
[[ 7.49398438e+02]
[ 1.65405273e-01]
[ -4.68750000e+00]
[ -4.79453125e+01]
[ -5.34570312e+00]]
With Julia:
X = [1 2104 5 1 45; 1 1416 3 2 40; 1 1534 3 2 30; 1 852 2 1 36]
y = [460; 232; 315; 178]
θ = ((X' * X)^-1) * X' * y
Result:
5-element Array{Float64,1}:
207.867
0.0693359
134.906
-77.0156
-7.81836
Furthermore, when I multiple X by Julia's — but not Python's — θ, I get numbers close to y.
I can't figure out what I am doing wrong. Thanks!
Using X^-1 vs the pseudo inverse
pinv(X) which corresponds to the pseudo inverse is more broadly applicable than inv(X), which X^-1 equates to. Neither Julia nor Python do well using inv, but in this case apparently Julia does better.
but if you change the expression to
julia> z=pinv(X'*X)*X'*y
5-element Array{Float64,1}:
188.4
0.386625
-56.1382
-92.9673
-3.73782
you can verify that X*z = y
julia> X*z
4-element Array{Float64,1}:
460.0
232.0
315.0
178.0
A more numerically robust approach in Python, without having to do the matrix algebra yourself is to use numpy.linalg.lstsq to do the regression:
In [29]: np.linalg.lstsq(X, y)
Out[29]:
(array([[ 188.40031942],
[ 0.3866255 ],
[ -56.13824955],
[ -92.9672536 ],
[ -3.73781915]]),
array([], dtype=float64),
4,
array([ 3.08487554e+03, 1.88409728e+01, 1.37100414e+00,
1.97618336e-01]))
(Compare the solution vector with #waTeim's answer in Julia).
You can see the source of the ill-conditioning by printing the matrix inverse you're calculating:
In [30]: np.linalg.inv(X.T.dot(X))
Out[30]:
array([[ -4.12181049e+13, 1.93633440e+11, -8.76643127e+13,
-3.06844458e+13, 2.28487459e+12],
[ 1.93633440e+11, -9.09646601e+08, 4.11827338e+11,
1.44148665e+11, -1.07338299e+10],
[ -8.76643127e+13, 4.11827338e+11, -1.86447963e+14,
-6.52609055e+13, 4.85956259e+12],
[ -3.06844458e+13, 1.44148665e+11, -6.52609055e+13,
-2.28427584e+13, 1.70095424e+12],
[ 2.28487459e+12, -1.07338299e+10, 4.85956259e+12,
1.70095424e+12, -1.26659193e+11]])
Eeep!
Taking the dot product of this with X.T leads to a catastrophic loss of precision.
Notice that X is a 4x5 matrix or in statistical terms that you have fewer observations than parameters to estimate. Therefore, the least squares problem has infinitely many solutions with the sum of the squared errors exactly equal to zero. In this case, the normal equations don't help you much because the matrix X'X is singular. Instead, you should just find a solution to X*b=y.
Most numerical linear algebra systems are based on the FORTRAN package LAPACK which uses the a pivoted QR factorization for solving the problem X*b=y. Since there are infinitely many solutions, LAPACK's picks the solution with the smallest norm. In Julia, you can get this solution, simply by writing
float(X)\y
(Unfortunately, the float part is necessary right now, but that will change.)
In exact arithmetic, you should get the same solution as the one above with either of your proposed methods, but the floating point representation of you problem introduces small rounding errors and these errors will affect the calculated solution. The effect of the rounding errors on the solution is much larger when using the normal equations compared to using the QR factorization directly on X.
This holds true also in the usual case where X has more rows than columns so often it is recommended that you avoid the normal equations when solving least squares problems. However, when X has many more rows than columns, the matrix X'X is relatively small. In this case, it will be much faster to solve the problem with the normal equations instead of using the QR factorization. In many statistical problems, the extra numerical error is extremely small compared to the statical error so the loss of precision due to the normal equations can simply be ignored.
I am looking to solve a problem of the type: Aw = xBw where x is a scalar (eigenvalue), w is an eigenvector, and A and B are symmetric, square numpy matrices of equal dimension. I should be able to find d x/w pairs if A and B are d x d. How would I solve this in numpy? I was looking in the Scipy docs and not finding anything like what I wanted.
For real symmetric or complex Hermitian dense matrices, you can use scipy.linalg.eigh() to solve a generalized eigenvalue problem. To avoid extracting all the eigenvalues you can specify only the desired ones by using subset_by_index:
from scipy.linalg import eigh
eigvals, eigvecs = eigh(A, B, eigvals_only=False, subset_by_index=[0, 1, 2])
One could use eigvals_only=True to obtain only the eigenvalues.
Have you seen scipy.linalg.eig? From the documentation:
Solve an ordinary or generalized eigenvalue problem of a square matrix.
This method have optional parameter b:
scipy.linalg.eig(a, b=None, ...
b : (M, M) array_like, optional
Right-hand side matrix in a generalized eigenvalue problem.
Default is None, identity matrix is assumed.
How do I get the inverse of a matrix in python? I've implemented it myself, but it's pure python, and I suspect there are faster modules out there to do it.
You should have a look at numpy if you do matrix manipulation. This is a module mainly written in C, which will be much faster than programming in pure python. Here is an example of how to invert a matrix, and do other matrix manipulation.
from numpy import matrix
from numpy import linalg
A = matrix( [[1,2,3],[11,12,13],[21,22,23]]) # Creates a matrix.
x = matrix( [[1],[2],[3]] ) # Creates a matrix (like a column vector).
y = matrix( [[1,2,3]] ) # Creates a matrix (like a row vector).
print A.T # Transpose of A.
print A*x # Matrix multiplication of A and x.
print A.I # Inverse of A.
print linalg.solve(A, x) # Solve the linear equation system.
You can also have a look at the array module, which is a much more efficient implementation of lists when you have to deal with only one data type.
Make sure you really need to invert the matrix. This is often unnecessary and can be numerically unstable. When most people ask how to invert a matrix, they really want to know how to solve Ax = b where A is a matrix and x and b are vectors. It's more efficient and more accurate to use code that solves the equation Ax = b for x directly than to calculate A inverse then multiply the inverse by B. Even if you need to solve Ax = b for many b values, it's not a good idea to invert A. If you have to solve the system for multiple b values, save the Cholesky factorization of A, but don't invert it.
See Don't invert that matrix.
It is a pity that the chosen matrix, repeated here again, is either singular or badly conditioned:
A = matrix( [[1,2,3],[11,12,13],[21,22,23]])
By definition, the inverse of A when multiplied by the matrix A itself must give a unit matrix. The A chosen in the much praised explanation does not do that. In fact just looking at the inverse gives a clue that the inversion did not work correctly. Look at the magnitude of the individual terms - they are very, very big compared with the terms of the original A matrix...
It is remarkable that the humans when picking an example of a matrix so often manage to pick a singular matrix!
I did have a problem with the solution, so looked into it further. On the ubuntu-kubuntu platform, the debian package numpy does not have the matrix and the linalg sub-packages, so in addition to import of numpy, scipy needs to be imported also.
If the diagonal terms of A are multiplied by a large enough factor, say 2, the matrix will most likely cease to be singular or near singular. So
A = matrix( [[2,2,3],[11,24,13],[21,22,46]])
becomes neither singular nor nearly singular and the example gives meaningful results... When dealing with floating numbers one must be watchful for the effects of inavoidable round off errors.
For those like me, who were looking for a pure Python solution without pandas or numpy involved, check out the following GitHub project: https://github.com/ThomIves/MatrixInverse.
It generously provides a very good explanation of how the process looks like "behind the scenes". The author has nicely described the step-by-step approach and presented some practical examples, all easy to follow.
This is just a little code snippet from there to illustrate the approach very briefly (AM is the source matrix, IM is the identity matrix of the same size):
def invert_matrix(AM, IM):
for fd in range(len(AM)):
fdScaler = 1.0 / AM[fd][fd]
for j in range(len(AM)):
AM[fd][j] *= fdScaler
IM[fd][j] *= fdScaler
for i in list(range(len(AM)))[0:fd] + list(range(len(AM)))[fd+1:]:
crScaler = AM[i][fd]
for j in range(len(AM)):
AM[i][j] = AM[i][j] - crScaler * AM[fd][j]
IM[i][j] = IM[i][j] - crScaler * IM[fd][j]
return IM
But please do follow the entire thing, you'll learn a lot more than just copy-pasting this code! There's a Jupyter notebook as well, btw.
Hope that helps someone, I personally found it extremely useful for my very particular task (Absorbing Markov Chain) where I wasn't able to use any non-standard packages.
You could calculate the determinant of the matrix which is recursive
and then form the adjoined matrix
Here is a short tutorial
I think this only works for square matrices
Another way of computing these involves gram-schmidt orthogonalization and then transposing the matrix, the transpose of an orthogonalized matrix is its inverse!
Numpy will be suitable for most people, but you can also do matrices in Sympy
Try running these commands at http://live.sympy.org/
M = Matrix([[1, 3], [-2, 3]])
M
M**-1
For fun, try M**(1/2)
If you hate numpy, get out RPy and your local copy of R, and use it instead.
(I would also echo to make you you really need to invert the matrix. In R, for example, linalg.solve and the solve() function don't actually do a full inversion, since it is unnecessary.)