Ask Your Question

# 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...

Thank you in advance.

edit retag close merge delete

## 2 answers

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

## Comments

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

( 2016-01-20 03:43:22 -0600 )edit

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.

( 2016-01-20 07:34:01 -0600 )edit

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2016-01-19 05:05:44 -0600

Seen: 926 times

Last updated: Jan 19 '16