scipy.sparse.linalg.bicg for Non Symmetric Matrix.
![](https://secure.gravatar.com/avatar/b0a57189571d15aa62f337b36cd4adf4.jpg?s=120&d=mm&r=g)
Hi, Am using the iterative methods of scipy.sparse.linalg for solving a linear system of equations Ax = b. My matrix A is non symmetric. I've been using the scipy.sparse.linalg.cg() function multiplying both matrix "A" and "b" with the transpose of A so the matrix will become symmetric: Asym = matrix(dot(A.T,A)) bsym = matrix(dot(A.T,b)) sol = cg(A,b,tol = 1e-10,maxiter=30) Also i've been reading about the biconjugate gradient method, and if i am not mistaken the literature says that this method works on non-symmetric matrix, but when i try to use scipy.sparse.linalg.bicg() it wont work: sol = bicg(A,b,tol = 1e-10,maxiter=30) File "C:\Python26\lib\site-packages\scipy\sparse\linalg\isolve\iterative.py", line 74, in bicg A,M,x,b,postprocess = make_system(A,M,x0,b,xtype) File "C:\Python26\lib\site-packages\scipy\sparse\linalg\isolve\utils.py", line 65, in make_system raise ValueError('expected square matrix (shape=%s)' % shape) NameError: global name 'shape' is not defined Any help will be greatly appreciated, am comparing this methods for my data with an important emphasis on speed for my master thesis, so any discrepancy with the theory would be a great problem for me. Thanks in advance, Raúl Acuña.
![](https://secure.gravatar.com/avatar/b0a57189571d15aa62f337b36cd4adf4.jpg?s=120&d=mm&r=g)
I made a typing error in the previous post, the first code segment is this one instead: Asym = matrix(dot(A.T,A)) bsym = matrix(dot(A.T,b)) sol = cg(Asym,bsym,tol = 1e-10,maxiter=30) On Thu, Dec 9, 2010 at 9:12 PM, Raul Acuña <raultron@gmail.com> wrote:
Hi,
Am using the iterative methods of scipy.sparse.linalg for solving a linear system of equations Ax = b. My matrix A is non symmetric. I've been using the scipy.sparse.linalg.cg() function multiplying both matrix "A" and "b" with the transpose of A so the matrix will become symmetric:
Asym = matrix(dot(A.T,A)) bsym = matrix(dot(A.T,b)) sol = cg(A,b,tol = 1e-10,maxiter=30)
Also i've been reading about the biconjugate gradient method, and if i am not mistaken the literature says that this method works on non-symmetric matrix, but when i try to use scipy.sparse.linalg.bicg() it wont work:
sol = bicg(A,b,tol = 1e-10,maxiter=30)
File "C:\Python26\lib\site-packages\scipy\sparse\linalg\isolve\iterative.py", line 74, in bicg A,M,x,b,postprocess = make_system(A,M,x0,b,xtype) File "C:\Python26\lib\site-packages\scipy\sparse\linalg\isolve\utils.py", line 65, in make_system raise ValueError('expected square matrix (shape=%s)' % shape) NameError: global name 'shape' is not defined
Any help will be greatly appreciated, am comparing this methods for my data with an important emphasis on speed for my master thesis, so any discrepancy with the theory would be a great problem for me.
Thanks in advance,
Raúl Acuña.
-- Ing. Raúl Acuña Profesor @Universidad Simón Bolívar Grupo de Mecatrónica Departamento de Electrónica y Circuitos Tel. +58-212-4121983 / Cel +58-412-5840317
![](https://secure.gravatar.com/avatar/da3a0a1942fbdc5ee9a9b8115ac5dae7.jpg?s=120&d=mm&r=g)
Thu, 09 Dec 2010 21:12:28 -0430, Raul Acuña wrote:
Am using the iterative methods of scipy.sparse.linalg for solving a linear system of equations Ax = b. My matrix A is non symmetric. I've been using the scipy.sparse.linalg.cg() function multiplying both matrix "A" and "b" with the transpose of A so the matrix will become symmetric:
Asym = matrix(dot(A.T,A)) bsym = matrix(dot(A.T,b)) sol = cg(A,b,tol = 1e-10,maxiter=30) [clip] sol = bicg(A,b,tol = 1e-10,maxiter=30) [clip] raise ValueError('expected square matrix (shape=%s)' % shape) [clip]
The error is saying that your matrix is not a square matrix. For a non-square matrix A, (A.T*A) is a square matrix, so that's why the CG algorithm works. So it's not about symmetry of the matrix. You should check that you have the same number of equations as unknowns. Otherwise the solution either does not exists or is not unique. -- Pauli Virtanen
![](https://secure.gravatar.com/avatar/25139367d600eddefb7f209a52410cf6.jpg?s=120&d=mm&r=g)
On 12/10/2010 01:51 PM, Pauli Virtanen wrote:
Thu, 09 Dec 2010 21:12:28 -0430, Raul Acuña wrote:
Am using the iterative methods of scipy.sparse.linalg for solving a linear system of equations Ax = b. My matrix A is non symmetric. I've been using the scipy.sparse.linalg.cg() function multiplying both matrix "A" and "b" with the transpose of A so the matrix will become symmetric:
Asym = matrix(dot(A.T,A)) bsym = matrix(dot(A.T,b)) sol = cg(A,b,tol = 1e-10,maxiter=30)
[clip]
sol = bicg(A,b,tol = 1e-10,maxiter=30)
[clip]
raise ValueError('expected square matrix (shape=%s)' % shape)
[clip]
The error is saying that your matrix is not a square matrix. For a non-square matrix A, (A.T*A) is a square matrix, so that's why the CG algorithm works. So it's not about symmetry of the matrix.
You should check that you have the same number of equations as unknowns. Otherwise the solution either does not exists or is not unique.
Note the NameError in the original post as well though -- that is, there may be a bug in the exception raising code as well. Dag Sverre
![](https://secure.gravatar.com/avatar/b0a57189571d15aa62f337b36cd4adf4.jpg?s=120&d=mm&r=g)
Thank you all, i saw my mistake. On Fri, Dec 10, 2010 at 9:00 AM, Pauli Virtanen <pav@iki.fi> wrote:
Fri, 10 Dec 2010 13:57:25 +0100, Dag Sverre Seljebotn wrote: [clip]
Note the NameError in the original post as well though -- that is, there may be a bug in the exception raising code as well.
Seems to have been fixed in r6780
_______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
-- Ing. Raúl Acuña Profesor @Universidad Simón Bolívar Grupo de Mecatrónica Departamento de Electrónica y Circuitos Tel. +58-212-4121983 / Cel +58-412-5840317
participants (3)
-
Dag Sverre Seljebotn
-
Pauli Virtanen
-
Raul Acuña