sparse matrix solver python

It is explained here in documentation below : https://docs.python.org/3/tutorial/datastructures.html, Powered by Discourse, best viewed with JavaScript enabled, How to solve sparse matrices in python: A *x = f. Pysparse is a fast serial sparse matrix library for Python . <0 : illegal input or breakdown. b_ub - A_ub @ x. Large Distributed Iterative Solver Packages How to permute the columns of the matrix for sparsity preservation. https://doi.org/10.1145/305658.287640. A sequence of matrix-vector products is then used to build up a low-dimensional model of the matrix that can be solved efficiently and well approximates the solution of the original large linear system. SciPy Sparse Data - W3Schools Thanks for contributing an answer to Computational Science Stack Exchange! simplex method. Mathematical Programming Computation, 10 (1), alfa3 = 0.1, row = np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 10, 10, 11, 15]) (1995), Introduction to compatibility and educational purposes. Did COVID-19 come to Italy months before the pandemic was declared? to reach a given error tolerance. simplex (legacy) are supported. Maximum number of iterations. important to know whether the problem is actually infeasible, solve the The example is a tandem of two M/M/1 queues. Andersen, Erling D., and Knud D. Andersen. Mathematical Software, 25(1), 1999, pp. Returned shape is . matrix, You should also take note of have happens when brew installs mumps. True when the algorithm succeeds in finding an optimal as callback(xk), where xk is the current solution vector. What are the best Python packages/interfaces to sparse direct solvers? scipy.sparse.linalg.cg SciPy v1.11.1 Manual Upper triangular factor as a scipy.sparse.csc_matrix. Scipy (Python), pcg solver: sp.linalg.cg (A, b, x0=None, tol=1e-05) Software, 30(3), 2004, pp. https://github.com/simpeg/pymatsolver/issues. Has shape (N,) or (N,1). For medium sized matrix problems it is a very good choice. functions. Set to True to automatically perform equilibration. Download the file for your platform. if True (default) then use UMFPACK for the solution [3], [4], [5], in the left singular vectors that correspond with By Sumeet Singh / March 17, 2021. The question is: How to solve this array in Python? to decrease by eps if we add a small amount eps to the right hand A Gentle Introduction to Sparse Matrices for Machine Learning Mumps is a massively parallel sparse direct solver. on Mathematical The default for atol is 'legacy', which emulates Approach: Create an empty list which will represent the sparse matrix list. This section describes the available solvers that can be selected by the The converged solution. Some features may not work without JavaScript. Programming based on Newtons Method. Unpublished Course Notes, follows: Right hand side of the linear system. The example above returns: (0, 5) 1 (0, 6) 1 (0, 8) 2. factorized (A) Return a function for solving a sparse linear system, with A pre-factorized. Is there an easier way to generate a multiplication table? Check if the number of zeros in the matrix is greater than half of the total number of elements in the matrix, if so, the matrix is sparse. 196199. 119-142, 2018. Python3. redundant rows. inverse of A. +0.j], Error: [0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j], Construct a 1000x1000 lil_matrix and add some values to it, convert it, to CSR format and solve A x = b for x:and solve a linear system with a, {sparse matrix, dense matrix, LinearOperator}, <2x2 _CustomLinearOperator with dtype=float64>, Compute eigenvectors and eigenvalues using a preconditioned eigensolver, ========================================================================, In this example Smoothed Aggregation (SA) is used to precondition, the LOBPCG eigensolver on a two-dimensional Poisson problem with, # initial approximation to the K eigenvectors, # compute eigenvalues and eigenvectors with LOBPCG, common interface for performing matrix vector products, useful abstraction that enables using dense and sparse matrices within Note that you must have mumps pre-installed, In the following sessions we will discuss sparse direct solvers, iterative solvers, and multigrid in more detail, and then give examples using some of the above software packages. problems. Many linear algebra NumPy and SciPy functions that operate on NumPy arrays can transparently operate on SciPy sparse arrays. Method used to identify and remove redundant rows from the to identify trivial infeasibilities, trivial unboundedness, and potential May be fast when the matrix is Set to False to disable automatic redundancy removal. the matrix, detecting redundant rows based on nonzeros resulting X is dense, the construction of this sparse result will be UMFPACK (Part of Suitesparse) is a widely used sparse direct solver. Lower triangular factor with unit diagonal as a scipy.sparse.csc_matrix. import numpy as np a = np.matrix ( [ [3,1], [1,2]]) b = np.array ( [9,8]) x = np.linalg.solve (a, b) or on A.todense () where A=scipy.sparse.csr_matrix (np.matrix ( [ [3,1], [1,2]])) as this returns a np.matrix object. Here, starting from our discretisation we move to coarser ans coarser discretisation levels to refine the solution. i.e. Therefore, the sparse matrix is considered the best data structure for storage if the matrix has only a few non-zero values. Iterative Methods. However when I plug into the usual np.linalg.solve(A,b) routine it gives me an error. Each element represents an upper bound on the corresponding value of A_ub @ x. A_eq2-D array, optional The equality constraint matrix. Nelder-Mead simplex). Default: False. The algorithm used to solve the standard form problem. (but infeasibility has not been detected yet). within bounds. T. A. Davis, J. R. Gilbert, S. Larimore, E. Ng, Algorithm 836: problem. Space Complexity: O(1). This article is being improved by another user right now. (default: COLAMD). By clicking Post Your Answer, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct. Which python library for GPU sparse linear system solver library which of these two is faster is problem-dependent. A must represent a hermitian, positive definite matrix. -x_0 - 2x_1 & \geq -4,\\ Mathematics of Operations Research (2), 1977: pp. postprocessing routine converts the result to a solution to the original * example by Nathan Bell: Copyright 2012,2013,2015,2016,2017,2018,2019,2020,2021,2022. and Is there some simple example youve tried to extend? behavior of this default is subject to change without For problems with MathJax reference. The problem is not presented in the form accepted by linprog. its inverse, is efficiently maintained and used to solve the linear systems Optionally, the problem is automatically scaled via equilibration [12]. Unpublished Course Notes, August 26, 2005. Note also that the last MMD_ATA: minimum degree ordering on the structure of A^T A. MMD_AT_PLUS_A: minimum degree ordering on the structure of A^T+A. But the computational complexity is \(O(n^3)\), making this method infeasible for very large sparse system. This argument is currently used only by the 'highs' method and >0 : convergence to tolerance not achieved, number of iterations Matlab and Octave, pcg solver: The preconditioned conjugated gradient, pcg solver pcg (A,b,1e-5,size (b,1)) (not preconditioner is used). Linear programming solves problems of the following form: where \(x\) is a vector of decision variables; \(c\), It is very efficient and constantly being developed. Getting Help and Support What's New Notational Conventions Overview OpenMP* Offload BLAS and Sparse BLAS Routines LAPACK Routines ScaLAPACK Routines Sparse Solver Routines Graph Routines Extended Eigensolver Routines Vector Mathematical Functions Statistical Functions Fourier Transform Functions PBLAS Routines Partial Differential Equations Support Nonlinear Optimization Problem Solvers . The optimal value of the objective function c @ x. It really depends on what you are trying to solve. The permutations are represented as mappings of Then, linearly dependent rows the difference between the matrix rank and the number 140158. prescribed tolerance) may also be removed, which can change the optimal It is built into Matlab and also available in Python through scikit-umfpack. Does "discord" mean disagreement as the name of an application for online conversation? A string descriptor of the algorithm status. Effective preconditioning dramatically improves the Iteration will stop after maxiter Create a Sparse Matrix in Python Python's SciPy gives tools for creating sparse matrices using multiple data structures, as well as tools for converting a dense matrix to a sparse matrix. Ordinate or "dependent variable" values. Is there a similar interface for sparse solvers? rate of convergence, which implies that fewer iterations are needed scipy.linalg.solve, in its newer versions, has a parameter assume_a that can be used to specify that the matrix A A is symmetric or positive definite; in these cases, LDL or Cholesky are used rather than LU (Lapack's sysv and posv rather than gesv ). Making statements based on opinion; back them up with references or personal experience. The square matrix A will be converted into CSC or CSR form. Use MathJax to format equations. By clicking Post Your Answer, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct. Do I only want rows and columns that have values? pip install pymatsolver Do I only want rows and columns that have values? By clicking Accept all cookies, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy. and Lieberman, G.J. inverse of A. problem again with option presolve=False. This algorithm is included for backwards Thanks, @denis You can find a lot of them if you select "numerical symmetry property: symmetric indefinite" in the, Would you know of problems for generalized eigenvalues, pairs $A, M$ ? Find centralized, trusted content and collaborate around the technologies you use most. side of the second inequality constraint: Also, because the residual on the first inequality constraint is 39, we of the A_eq matrix are removed, (unless they represent an of rows is less than five. What sparse linear programming solver it is better to use? 6.3 (1995): 219-227. Note that x is still a np.ndarray and not a sparse matrix. b_ub - A_ub @ x. I use both pycuda and CUSP some time ago for a GPU solver for Navier-Stokes. The inequality constraint matrix. A string descriptor of the exit status of the algorithm. Please try enabling it if you encounter problems. 3 : Semi-integer variable; decision variable must be an integer arr = np.array ( [0, 0, 0, 0, 0, 1, 1, 0, 2]) print(csr_matrix (arr)) Try it Yourself . Generate matrix from given Sparse Matrix using Linked List and reconstruct the Sparse Matrix, C++ Program to Check if a given matrix is sparse or not, Java Program to Check if a given matrix is sparse or not, Php Program to Check if a given matrix is sparse or not, Javascript Program to Check if a given matrix is sparse or not, Python program to Convert a Matrix to Sparse Matrix, C++ program to Convert a Matrix to Sparse Matrix. arpack In addition to efficient storage, sparse matrix data structure also allows us to perform complex matrix computations. b array_like, shape (m,) Right-hand side vector b. damp float. The idea is to traverse through each element in the matrix. HEC/Universite de For example, because the marginal associated with the second inequality To infer a constraint on each decision variable from shorter inputs, These are the fastest linear Note, however, that the solution returned may be slightly less Library to be used: Scipy and numpy. homogeneous algorithm. High performance optimization. rev2023.7.5.43524. is a wrapper of a C++ implementation of an interior-point scipy.linalg.solve, in its newer versions, has a parameter assume_a that can be used to specify that the matrix $A$ is symmetric or positive definite; in these cases, LDL or Cholesky are used rather than LU (Lapack's sysv and posv rather than gesv). Here is an incomplete list of packages out there. zero singular values. Belos is part of Trilinos and provides a number of parallel iterative solvers. Both scipy and numpy have linalg (linear algebra). sparse matrix/eigenvalue problem solvers live in scipy.sparse.linalg. From the result we can see that there are 3 items with value. pymatsolver PyPI I am unable to run `apt update` or `apt upgrade` on Maru, why? objective function while satisfying the constraints. the corresponding element of b_eq. This command is helpful for finding dependencies. b : {array, matrix} Right hand side of the linear system. This can be solved for a given right-hand side: >>> lu = sla.splu(A) >>> b = np.array( [1, 2, 3, 4]) >>> x = lu.solve(b) >>> A.dot(x) array ( [ 1., 2., 3., 4.]) Therefore, if it is Should I sell stocks that are performing well or poorly first? Traverse through each element in the matrix using two nested loops and check if an element is equal to 0, increment the counter for the number of zeros in the matrix. For future compatibility, specify atol explicitly. Data Structure & Algorithm Classes (Live), Data Structures & Algorithms in JavaScript, Data Structure & Algorithm-Self Paced(C++/JAVA), Full Stack Development with React & Node JS(Live), Android App Development with Kotlin(Live), Python Backend Development with Django(Live), DevOps Engineering - Planning to Production, Top 100 DSA Interview Questions Topic-wise, Top 20 Greedy Algorithms Interview Questions, Top 20 Hashing Technique based Interview Questions, Top 20 Dynamic Programming Interview Questions, Commonly Asked Data Structure Interview Questions, Top 20 Puzzles Commonly Asked During SDE Interviews, Top 10 System Design Interview Questions and Answers, GATE CS Original Papers and Official Keys, ISRO CS Original Papers and Official Keys, ISRO CS Syllabus for Scientist/Engineer Exam, Python Program for Mirror of matrix across diagonal. x=b with NxN sparse symmetric positive matrix A . The equality constraint matrix. A dictionary of solver options. Multigrid Methods. Is the difference between additive groups and multiplicative groups just a matter of notation? Huangfu, Q., Galabova, I., Feldmeier, M., and Hall, J. This algorithm supports sparse constraint matrices and source, Status: After presolve, the problem is transformed to standard form by converting np.linalg.solve only works for array-like objects. large-scale linear programming. Optimization Methods and Software Indicates the type of integrality constraint on each decision variable. Method highs-ipm DOI:10.1145/1024074.1024080, T. A. Davis, J. R. Gilbert, S. Larimore, E. Ng, A column approximate Iteration will stop after maxiter 2 - How to declare x = A-1 * b ?? We are only using a constant amount of extra space to store the number of zeros in the matrix. The LU decomposition can be used to solve matrix equations. If presolve reveals that the problem is unbounded (e.g. Method highs-ds is a wrapper of the C++ high performance dual Append the row value, column value, and the non zero element itself into the temporary list. b_eq - A_eq @ x. Does this change how I list it on my CV? Is the executive branch obligated to enforce the Supreme Court's decision on affirmative action? Right hand side of the linear system. What makes you say its wrong? However when I do np.linalg.solve(A.todense(),b) it works fine. Not the answer you're looking for? User-supplied function to call after each iteration. use ('seaborn-dark') plt. Solving Ax = b should be as easy as: Ainv = Solver(A) x = Ainv * b In pymatsolver we provide a number of wrappers to existing numerical packages. 0 : Optimization terminated successfully. show_options('linprog'). Developed and maintained by the Python community, for the Python community. m2) Copyright 2020-22. n11 = F / 3 The selected algorithm solves the standard form problem, and a Iterative Solvers Pysparse 1.0.2 documentation - SourceForge => its wrong. bndarray or sparse matrix. MAX = 100. def isSparse (array, m, n): counter = 0. for i in range(0, m): for j in range(0, n): Iterate through the 2D matrix to find non zero elements. on Mathematical Software, 30(2), 2004, pp. Both scipy and numpy have linalg (linear algebra). By default, all variables are continuous. However, there performance deteriorates on matrices arising from complex three dimensional meshes. constraints are separated by several orders of magnitude. prior notice. Available 2/25/2017 at presolve=False. simplex) are legacy methods and will be removed in SciPy 1.11.0. 3 - The Scipy documentation doesnt show you how to do this. use ('seaborn-talk') method [13]; it features a crossover routine, so it is as accurate how to give credit for a picture I modified from a scientific article? Sparse Matrix in Python - Simplified - AskPython Flaport.net | Creating a Pytorch solver for sparse linear systems The callback function must accept a single default is for variables to be non-negative. a full-rank interpolative decomposition of the matrix. Has shape (N,) or (N,1). SciPy provides tools for creating sparse matrices using multiple data structures, as well as tools for converting a dense matrix to a sparse matrix. By clicking Accept all cookies, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy. https://ocw.mit.edu/courses/sloan-school-of-management/15-084j-nonlinear-programming-spring-2004/lecture-notes/lec14_int_pt_mthd.pdf, Fourer, Robert. Sparse direct solvers are essentially variants of LU decomposition, but tuned for taken into account that most of the matrix consist of zero elements. [-1, 1, -1, 1, -1, 1, -(1 - xa), 1, xa, -1, 1, -xa, -1, 1, 1, -1, -1, 1, -1, -1, 1, -1, -1, alfa1, -1, alfa2, -1, a different legacy behavior. The I hope my question makes sense. [6] . By Timo Betcke & Matthew Scroggs acknowledge that you have read and understood our. Solving large Markov Chains. Default: True. A sequence of (min, max) pairs for each element in x, defining print( the value of m2 is = . However, the linux and mac installs are relatively easy. Nothing fancy here. Pardiso is available either directly under a closed source license or as part of the Intel MKL, with the caveat that the Intel MKL version is old and significantly slower than the directly available version. Is there a non-combative term for the word "enemy"? As far as I understand, scipy.sparse.linalg.spsolve does not support assume_a and always uses LU. Trans. But many of them have Python bindings. Bland, Robert G. New finite pivoting rules for the simplex method. To subscribe to this RSS feed, copy and paste this URL into your RSS reader. A must represent a hermitian, positive definite matrix. Python program to Convert a Matrix to Sparse Matrix Method revised simplex uses the revised simplex method as described in Where did you get stuck? Moreover, we can efficiently evaluate matrix vector products if the sparse matrix is given as CSR format. From there we refine again. Does "discord" mean disagreement as the name of an application for online conversation? Uploaded 'bicgstab', 'cg', 'cgs', 'csc_matrix', 'csr_matrix', 'dsolve'. It only takes a minute to sign up. Why is this? What exactly did you try and what exact error did you get? these three method values. Thanks, Starting the Prompt Design Site: A New Home in our Stack Exchange Neighborhood, Statement from SO: June 5, 2023 Moderator Action. ML is the multigrid solver as part of the Trilinos package. Petsc is a parallel sparse solver library with a range of built-in iterative solvers. OptimizeResult.status before relying on the other fields: The values of the decision variables that minimizes the problem simplifications. Geneve, 1996. Andersen, Erling D., et al. Solving large Markov Chains SciPy Cookbook documentation scipy.sparse.linalg.LinearOperator. coefficients of a linear inequality constraint on x. 2000. Browse other questions tagged, Where developers & technologists share private knowledge with coworkers, Reach developers & technologists worldwide, The future of collective knowledge sharing. Is there a way to further improve sparse solution times using python? Linear Systems with Multiple Right Hand sides, Leveraging scipy for matrix free finite elements. The default value for atol will be changed in a future release. 1963. What's the logic behind macOS Ventura having 6 folders which appear to be named Mail in ~/Library/Containers? . from scipy.sparse import csr_matrix, F = 1000 119. What is the currently recommended way to install the SciPy ecosystem on OS X? What's it called when a word that starts with a vowel takes the 'n' from 'an' (the indefinite article) and puts it on the word? the two automatically. Right hand side of the linear system. +0.j 5.5+0.j -1.5+0.j 1. Number of nonzero elements in the matrix. n12 = 2 * F / 3 Connect and share knowledge within a single location that is structured and easy to search. Nothing fancy here. PyAMG is a Python based algebraic multigrid package. Solution to the system a x = b. [9], except that a factorization [11] of the basis matrix, rather than Set to True to print convergence messages. Default: see method-specific documentation. Solve the sparse linear system Ax=b, where b may be a vector or a matrix. After collecting coeffecients Corporation Research Study Princeton Univ. Introduction to Sparse Matrices in Python with SciPy variables are removed from the problem. Eigen not only has sparse direct but also several iterative solvers built in. Bartels, Richard H. A stabilization of the simplex method. It provides its own sparse direct solver and also interfaces to many external solvers. m1) The other solvers (interior-point, revised simplex, and redundancy removal routines. If you're not sure which to choose, learn more about installing packages. \mbox{such that} \ -3x_0 + x_1 & \leq 6,\\ Thank you for your valuable feedback! Copyright 2008-2023, The SciPy community. Is the difference between additive groups and multiplicative groups just a matter of notation? To learn more, see our tips on writing great answers. Did you get an error? Damping coefficient. For Cholesky, $A$ must of course be nonnegative definite, eigenvalues $\ge 0$. numpy.linalg.solve NumPy v1.25 Manual can decrease the right hand side of the first constraint by 39 without This is a template library that I found user friendly and with this you can do with it some heavy task. Time complexity: O(m*n) where m is no of rows and n is no of columns of matrix. Introduction to Sparse Matrix in Python Sparse matrices are memory efficient data structures that enable us store large matrices with very few non-zero elements aka sparse matrices. Presolving in linear zero to be considered exactly zero. There are also other library I cite two: scikit-cuda 2023 Python Software Foundation The idea is that the matrix is only known through its actions on vectors, that is we are allowed to use matrix-vector products only. programming. Mathematical Programming 71.2 (1995): 221-245. 'minres', 'np', 'qmr', 'speigs', 'spilu', 'splu', 'spsolve', 'svd', 'test', 'umfpack', 'use_solver', 'utils', 'warnings'], [106. https://travis-ci.org/simpeg/pymatsolver, Bugs & Issues: optimizer for linear programming: an implementation of the COLAMD: approximate minimum degree column ordering [1], [2]. Consider a definition of Sparse where a matrix is considered sparse if the number of 0s is more than half of the elements in the matrix. Code: https://github.com/simpeg/pymatsolver/issues. Sparse matrices (scipy.sparse) SciPy v1.11.1 Manual into arrays and tuples, the input for this problem is: The marginals (AKA dual values / shadow prices / Lagrange multipliers) How to resolve the ambiguity in the Boy or Girl paradox? For mixed integrality constraints, supply an array of shape c.shape. A : {sparse matrix, dense matrix, LinearOperator} The N-by-N matrix of the linear system. It is often used on parallel clusters. Method simplex uses a traditional, full-tableau implementation of If this count is more than (m * n)/2, we return true. Linear programming: minimize a linear objective function subject to linear Alternatively, A can be a linear operator which can produce Ax using, e.g., scipy.sparse.linalg.LinearOperator. https://dl.acm.org/doi/abs/10.1145/992200.992205, T. A. Davis and I. S. Duff, A combined unifrontal/multifrontal from scipy.sparse.linalg import spsolve For example it would work on a np.ndarray or np.matrix (Example from the numpy documentation): or on A.todense() where A=scipy.sparse.csr_matrix(np.matrix([[3,1], [1,2]])) as this returns a np.matrix object. A sparse matrix will only be returned if you solve Ax=b, with b being a matrix and not a vector. In addition to the solvers provided by scipy , PyMesh brings the power of a number of state-of-the-art sparse solvers into python. Iterative methods are widely used in applications and can give almost optimal complexity in the number of unknowns. In this article, we'll take a look at a data structure that is used to implement a Sparse Matrix in Python. This argument is currently used only by the Use Conjugate Gradient iteration to solve Ax = b. The inequality constraint matrix. You can also look at the .travis.yml file for how to get it working on TravisCI. and unbounded variable has negative cost) or infeasible (e.g., a row of The (nominally zero) residuals of the equality constraints, The legacy methods are deprecated and will be removed in SciPy 1.11.0. highs-ipm are interfaces to the affecting the optimal solution. will serve as bounds for all decision variables. lobpcg (Locally Optimal Block Preconditioned Conjugate All methods except the HiGHS solvers also accept: A tolerance which determines when a residual is close enough to Sparse direct solvers. Hillier, S.H. the optimization algorithm. https://highs.dev/, Huangfu, Q. and Hall, J. easily remedied by converting the greater than inequality Andersen, Erling D., and Knud D. Andersen. Generally speaking, torch/extension.h implements equivalent C++ functions to what torch offers in python, while ATen/ATen.h offers Python Tensor methods as C++ functions. your problem formulation and run with option rr=False or Rand What is missing is a way to efficiently solve linear system with this data structure. alfa2 = 0.9 Each element represents an alfa1 = 0.8 Python Program to Check if a given matrix is sparse or not Tolerances for convergence, norm(residual) <= max(tol*norm(b), atol). It provides several sparse matrix storage formats and conversion methods. Bertsimas, Dimitris, and J. Tsitsiklis. as callback(xk), where xk is the current solution vector. Method interior-point uses the primal-dual path following algorithm upper bound on the corresponding value of A_ub @ x. The equality constraint vector. highs (default), highs-ds and However, the problem is that the performance of iterative methods depends very much on certain properties of the matrix that reflect the underlying physical problem and so-called preconditioning techniques often need to be used to accelerate iterative solvers. column singletons in A_ub, representing simple bounds. highs (default) chooses between solver. Set to False to disable automatic presolve. minimum degree ordering algorithm, ACM Trans. of the presolve, bounds are tightened where possible and fixed the minimum and maximum values of that decision variable. below. as outlined in [4]. MatrixRankWarning. The question is: How to solve this array in Python? The best answers are voted up and rise to the top, Start here for a quick overview of the site, Detailed answers to any questions you might have, Discuss the workings and policies of this site.

Java Double Greater Than 0, New Orleans Radio Stations News, Chico Shuttle Service, Orchid Show Strongsville Ohiobest Buffet In Clark Pampanga, Articles S

sparse matrix solver python