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.