# 1x1 matrix -> scalar

Hi everyone, i have a problem with dimensions: I am programing the conjugate gradient algorithm, unfortunatly, when the result is scalar, Sage still handles it a 1x1 matrix, and as a result i cannot use multiplication. How can i fix it, maybe there is a good tutorial or a book for Sage and linear algebra.

Here is my work so far:

https://cloud.sagemath.com/projects/1...

edit retag close merge delete

Sort by » oldest newest most voted 1 x 1 matrix A --> scalar a :

a =  A[0,0]

more

Hello

Of course you can not multiply 1x1 matrices with a 3x1 matrix... You should use vectors instead of matrix columns (they are considered as the same time as row and column vector and hence no need to transpose). The following works

B = Matrix([[-1,0,1], [0,-1,1], [1,0,1], [0,1,1], [-1,0,1]])
A = B.transpose()*B
c = vector([0,0,2,2,0])         # <- changed to vector
b = B.transpose()*c
x = vector([1,0,1])             # <- changed to vector
r = b-A*x
p = r
a = ((r.norm())^2)/(p*A*p)      # <- removed transpose
x = x+a*p

more

Thank you is there a short cut with brackets to create vectors and matrices?

Yes and no. You can do the following

sage: V = ZZ^4
sage: V([1,2,5,-1])
(1, 2, 5, -1)


But there can not be a shortcut in Sage and while being compatible with Python. Namely brackets are used for lists and paranthesis for tuples.