HP Forums
subscripts in CAS? - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: subscripts in CAS? (/thread-7416.html)



subscripts in CAS? - Wes Loewer - 12-12-2016 06:40 PM

In Maxima, you can use subscripts, such as
Code:
sum(a[k]*x^k, k, 0, 5);
which results in
Code:
a[5]*x^5+a[4]*x^4+a[3]*x^3+a[2]*x^2+a[1]*x+a[0]

or more general
Code:
sum(a[k]*x^k, k, 0, n);
where n is a function parameter and a[n] are unspecified constants.

Is there a way to use subscripts in a similar manner on the Prime?


RE: subscripts in CAS? - Han - 12-12-2016 08:57 PM

(12-12-2016 06:40 PM)Wes Loewer Wrote:  In Maxima, you can use subscripts, such as
Code:
sum(a[k]*x^k, k, 0, 5);
which results in
Code:
a[5]*x^5+a[4]*x^4+a[3]*x^3+a[2]*x^2+a[1]*x+a[0]

or more general
Code:
sum(a[k]*x^k, k, 0, n);
where n is a function parameter and a[n] are unspecified constants.

Is there a way to use subscripts in a similar manner on the Prime?

The exact command on the prime returns a similar looking polynomial except the notation at(a,k-1) is used in place of a[k] for display. This allows you to later store a vector/list into the variable a and the polynomial will be modified accordingly (on the display). You can, instead, use a(k) which will then be displayed as a(k) on the screen. In this case, a is implicitly considered a function.

sum(a(k)*x^k, k, 0, n);


RE: subscripts in CAS? - compsystems - 12-13-2016 02:40 AM

examples of use on CAS MODE, n>0

0: with Σ cmd
purge(a);
Σ(a[k]*x^k,k,1,3) -> x*a[1]+x^2*a[2]+x^3*a[3] // with [] at(a, k-1) == a[1]
Σ(a(k)*x^k,k,1,3) -> x*a(1)+x^2*a(2)+x^3*a(3) // with ()
a:=MAKELIST(x,x,1,3); -> {1,2,3}
Σ(a(k)*x^k,k,1,3) -> x+2*x^2+3*x^3

1: with SUM cmd
purge(a);
sum(a[k]*x^k,k,1,3); -> x*a[1]+x^2*a[2]+x^3*a[3] // with []
sum(a(k)*x^k,k,1,3); -> x*a(1)+x^2*a(2)+x^3*a(3) // with ()
a:=MAKELIST(x,x,1,3); -> {1,2,3}
sum(a(k)*x^k,k,1,3) -> x+2*x^2+3*x^3

2: with SEQ cmd
purge(a);
seq(a(k)*x^k,k,1,3); -> [x*a(1),x^2*a(2),x^3*a(3)]
a:=MAKELIST(x,x,1,3); -> {1,2,3}
seq(a(k)*x^k,k,1,3); ->[x,2*x^2,3*x^3]
ΣLIST( seq(a(k)*x^k, k,1,3)) -> x+2*x^2+3*x^3

---

I would like to see the notation using the character ↓ To differentiate it from other operations with parenthesis (evaluation) and so that pretty-print can decode more easily

seq(a↓(k)*x^k,k,1,5); ->
[x*a↓(1),x^2*a↓(2),x^3*a↓(3),x^4*a↓(4),x^5*↓a(5)]


RE: subscripts in CAS? - Wes Loewer - 12-15-2016 04:53 AM

What I had in mind was the ability to treat a[k] as you would any other identifier, such as solve([a[1]+a[2]+a[3]=1,a[1]^2+a[3]=2, a[2]-a[3]=3],[a[1],a[2],a[3]])

Perhaps it would be better to just show the application I was working on. The following is a function I wrote for Maxima which calculates the coefficients used for the various polynomial integration rules: Trapezoid Rule (n=1), Simpson's Rule (n=2), Simpson's 3/8 Rule (n=3), etc.

Notice how eqs and coefs are lists of equations and coefficients which are determined at run time. In particular, the linsolve() function uses the coefs list as the list of variables to solve for.

Code:

NewtonCotesWeights(n):=
block ( [a, coefs, eqs, sol, k, i, ret],
    p(x,n):=sum(a[k]*x^k, k, 1, n)+a[0],
    coefs(n):=makelist(a[k],k,0,n),
    eqs(n):=makelist(y[k]=p(k*h,n),k,0,n),
    sol:linsolve(eqs(n),coefs(n)),
    for i: 0 thru n do
      b[i]:=rhs(sol[i+1]),
    ret:factor(integrate(sum(b[k]*x^k, k, 1, n)+b[0],x,0,n*h)),
    for i: 0 thru n do
      kill(b[i]),
    ret
    );

NewtonCotesWeights(3);
(3*(y[3]+3*y[2]+3*y[1]+y[0])*h)/8

I've not been able to figure out how to do this on the Prime. It boils down to being able to treat a subscripted identifier as it would any other identifier.


RE: subscripts in CAS? - parisse - 12-15-2016 07:45 AM

Replacing makelist by seq seems to work:
Code:

p(x,n):=sum(a[k]*x^k, k, 1, n)+a[0];
coefs(n):=seq(a[k],k,0,n);
eqs(n):=seq(y[k]=p(k*h,n),k,0,n);
sol:=linsolve(eqs(n),coefs(n));
...
If you teach Lagrange polynomial interpolation before teaching Newton-Cotes methods, you can enter :
Code:

n:=2; 
p:=lagrange(seq(k*h,k,0,n),seq(y[k],k,0,n));
factor(int(p,x,0,n*h)/n)



RE: subscripts in CAS? - Wes Loewer - 12-15-2016 06:40 PM

(12-15-2016 07:45 AM)parisse Wrote:  Replacing makelist by seq seems to work:

Thanks. That's just what I was missing.


RE: subscripts in CAS? - compsystems - 12-17-2016 12:21 PM

Using brackets [], should index to zero and And parentheses in 1.

{a,b,c}(1)
-> a

{a,b,c}[0]
-> a

but
{a,b,c}[0] -> "Index outside range ..."

{a,b,c}(1) or {a,b,c}[1] -> a
So you have two things that do the same thing. for what?

Starting at zero, we avoid rewriting the expressions
sum(a[k]*x^k, k, 0, 5) -> sum(a[k]*x^k, k, 1, 6)