Ask Your Question

stablum's profile - activity

2023-08-11 09:44:19 +0200 received badge  Famous Question (source)
2020-07-31 18:05:23 +0200 received badge  Notable Question (source)
2020-04-16 12:22:39 +0200 received badge  Student (source)
2019-07-16 17:39:53 +0200 received badge  Popular Question (source)
2019-06-07 18:18:08 +0200 received badge  Notable Question (source)
2018-10-07 23:29:14 +0200 received badge  Notable Question (source)
2017-04-01 19:08:17 +0200 received badge  Popular Question (source)
2016-05-24 13:47:57 +0200 received badge  Taxonomist
2015-06-24 18:07:56 +0200 received badge  Popular Question (source)
2014-09-10 00:32:46 +0200 asked a question problems with product of vector of symbols with square matrix

Hi,

I am trying to do some experiments with symbols (variable vector) and multiplications with a coefficient matrix.

The code is the following:

A = matrix(QQ,[
    [2,1,2,-6],
    [-1,2,1,7],
    [3,-1,-3,-1],
    [1,5,6,0]
])

k = A.transpose().kernel()
basis = k.basis()[0]
t = 'real'
var('x1')
assume(x1,t)
var('x2')
assume(x2,t)
var('x3')
assume(x3,t)
var('x4')
assume(x4,t)

x = vector([x1,x2,x3,x4])
print "x",x
xT = x.transpose()
print "xT",xT
print "A*x",A*x
print "xT*A",xT*A

with the following output:

x (x1, x2, x3, x4)
xT [x1]
[x2]
[x3]
[x4]
A*x (2*x1 + x2 + 2*x3 - 6*x4, -x1 + 2*x2 + x3 + 7*x4, 3*x1 - x2 - 3*x3 - x4, x1 + 5*x2 + 6*x3)
xT*A
Traceback (most recent call last):    
  File "", line 1, in <module>

  File "/tmp/tmpuVBZ96/___code___.py", line 27, in <module>
    exec compile(u'print "xT*A",xT*A
  File "", line 1, in <module>

  File "element.pyx", line 2751, in sage.structure.element.Matrix.__mul__ (sage/structure/element.c:19587)
  File "coerce.pyx", line 856, in sage.structure.coerce.CoercionModel_cache_maps.bin_op (sage/structure    /coerce.c:8169)
TypeError: unsupported operand parent(s) for '*': 'Full MatrixSpace of 4 by 1 dense matrices over Symbolic Ring'     and 'Full MatrixSpace of 4 by 4 dense matrices over Rational Field'

As you can see, A*x was successful, but xT*A is giving an exception. Do you have any idea on why? How would you solve this?

2014-09-08 04:59:30 +0200 received badge  Teacher (source)
2014-09-08 04:59:30 +0200 received badge  Self-Learner (source)
2014-09-05 21:58:06 +0200 answered a question create matrix as having a subset of columns from another matrix

Just found how to do this in the easiest and most concise way:

A[:,p]
2014-09-05 21:52:40 +0200 asked a question create matrix as having a subset of columns from another matrix

I need to get a new matrix generated by selecting a subset of columns from another matrix, given a list (or tuple) of column indices.

The following is the code I am working on (there is a bit more than just the attempt to create a new matrix, but might be interesting for you to have some context).

A = matrix(QQ,[
[2,1,4,-1,2],
[1,-1,5,1,1],
[-1,2,-7,0,1],
[2,-1,8,-1,2]
])
print "A\n",A
print "A rref\n",A.rref()
p = A.pivots()
print "A pivots",p

with the following output:

A
[ 2  1  4 -1  2]
[ 1 -1  5  1  1]
[-1  2 -7  0  1]
[ 2 -1  8 -1  2]
A rref
[ 1  0  3  0  0]
[ 0  1 -2  0  0]
[ 0  0  0  1  0]
[ 0  0  0  0  1]
A pivots (0, 1, 3, 4)

Now I expected to find easily a method from matrix objects which allowed to construct a new matrix with a subset of columns by just giving the tuple p as parameter, but could not find anything like that.

Any ideas on how to solve this elegantly in a sage-friendly way? (avoiding for loops and excess code)

thanks!

2014-08-19 20:16:25 +0200 commented answer Newbie question: introducing symbols (variables) inside vectors and matrices

Thanks! It worked! :)

2014-08-19 20:16:04 +0200 received badge  Scholar (source)
2014-08-19 18:21:35 +0200 received badge  Editor (source)
2014-08-19 18:18:57 +0200 asked a question Newbie question: introducing symbols (variables) inside vectors and matrices

Hi,

I would like to have linear algebra operations to be evaluated with symbols instead of numerically. For example, having the following matrix:

A = matrix(QQ,[
    [2,1,2,-6],
    [-1,2,1,7],
    [3,-1,-3,-1],
    [1,5,6,0],
    [2,2,1,1]
    ])

I would like to multiply for a vector with symbolic variables as follows:

t = 'real'
var('x1')
assume(x1,t)
var('x2')
assume(x2,t)
var('x3')
assume(x3,t)
var('x4')
assume(x4,t)
xx = vector(QQ,[x1,x2,x3,x4])
A * xx.transpose()

Unfortunately building the xx vector is unsuccessful, producing this error message:

TypeError: Cannot evaluate symbolic expression to a numeric value.

This does not work, so how can I use symbols in sage's linear algebra framework?

thank you very much, -Francesco