[sage-support] Re: Matrix multiplication by symbolic vector

2021-06-06 Thread slelievre
Don't feel stupid, but learn from your mistakes.
Go beyond "this works / this doesn't work":

> [c[i] for i in range(0..2)] <-- this doesn't work

Read error messages, they contain useful information.
```
sage: [c[i] for i in range(0..2)]
---
TypeError Traceback (most recent call last)
 in 
> 1 [c[i] for i in range(ellipsis_iter(Integer(0),Ellipsis,Integer(2)))]

TypeError: 'generator' object cannot be interpreted as an integer
```
Here, you can see that
- `range(0..2)` was preparsed as
  `range(ellipsis_iter(Integer(0),Ellipsis,Integer(2)))`,
- we get a type error because something (here: range)
  expected an integer and got something else
This gives a clue of what went wrong: range wants
integer arguments: range(2), range(1, 3);  but not
range of an ellipsis iteration like `0 .. 2`.

Now, about the proposed code after fixing this range mistake.

After defining:
```
sage: R = PolynomialRing(RR, 'c', 20)
sage: c = R.gens()
sage: c = vector([c[i] for i in (0 .. 2)])
sage: Z1 = matrix([[1, 2], [3, 4]])*vector([c[1], c[2]])
```
we have:
```
sage: Z1
(c1 + 2.00*c2, 3.00*c1 + 4.00*c2)
sage: Z1[0]
c1 + 2.00*c2
```

This `Z1[0]` is a polynomial, not a symbolic expression:
```
sage: parent(Z1[0])
Multivariate Polynomial Ring in c0, c1, c2, ..., c18, c19
over Real Field with 53 bits of precision
```

So `== 0` tests whether it is zero:
```
sage: Z1[0] == 0
False
```
and does not give a symbolic equation.

To get an equation, convert `Z1[0]` to the symbolic ring:
```
sage: SR(Z1[0]) == 0
1.00*c1 + 2.00*c2 == 0
```

To use `solve`, both the equation and the variable
must be in the symbolic ring.

Converting polynomial variables over the floating-point reals `RR`
to symbolic variables is a little more tricky:
```
sage: SR(c[1])
1.00*c1
sage: SR(c[1]).variables()[0]
c1
```

>From there:
```
sage: solve(SR(Z1[0]) == 0, SR(c[1]).variables()[0])
[c1 == -2*c2]
```

Working over the integers or the rationals simplifies things a bit:
```
sage: R = PolynomialRing(QQ, 'c', 20)
sage: c = vector(R.gens())
sage: c[1:3]
(c1, c2)
sage: Z1 = matrix([[1, 2], [3, 4]])*c[1:3]
sage: Z1
(c1 + 2*c2, 3*c1 + 4*c2)
sage: Z1[0]
c1 + 2*c2
sage: SR(Z1[0])
c1 + 2*c2
sage: SR(c[1])
c1
sage: solve(SR(Z1[0]) == 0, SR(c[1]))
[c1 == -2*c2]
```

Note that for equations of the form `== 0`,
you can skip the `== 0` when calling `solve`,
feeding it only the left hand side:
```
sage: solve(SR(Z1[0]), SR(c[1]))
[c1 == -2*c2]
```

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sage-support/ef4c405c-7160-41fa-96c0-522fcde7cfa4n%40googlegroups.com.


[sage-support] Re: Matrix multiplication by symbolic vector

2021-06-06 Thread cyrille piatecki
OK, one more time I feel stupid and one more time thanks to answer. But now 
look at this

R = PolynomialRing(RR, 'c', 20)
c = R.gens()
c=vector([c[i] for i in (0..2)])
Z1=matrix([[1, 2],[3,4]])*vector([c[1],c[2]])
show(Z1)<--- correct display
But c and c[1] is not recognized. So I do not know how to call the c's.
solve(Z1[0]==0,c)

solve(Z1[0]==0,c[1])


On Sunday, June 6, 2021 at 12:14:15 PM UTC+2 slelievre wrote:

> 2021-06-06 10:47:24 UTC+2, Cyrille Piatecki:
> >
> > [c[i] for i in range(0..2)] <-- this doesn't work
>
> You are mixing two different options:
>
> - range:
>   [c[i] for i in range(2)]
>   [c[i] for i in range(0, 2)]
>
> - ellipsis:
>   [c[i] for i in (0 .. 2)]
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sage-support/827e65e0-f2ea-4ef7-bf6c-2cb9eeed105en%40googlegroups.com.


[sage-support] Re: Matrix multiplication by symbolic vector

2021-06-06 Thread slelievre
2021-06-06 10:47:24 UTC+2, Cyrille Piatecki:
>
> [c[i] for i in range(0..2)] <-- this doesn't work

You are mixing two different options:

- range:
  [c[i] for i in range(2)]
  [c[i] for i in range(0, 2)]

- ellipsis:
  [c[i] for i in (0 .. 2)]

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sage-support/099cbcb7-2455-4b4c-982e-7214c6fa507fn%40googlegroups.com.