I am trying to create formulae for products with, say, p sub k as symbols. These are the results from two, at least naively identical formulae:
(1) -> (1+p[1])*(1+p[2])
product(1+p[k],k=1..2)
But perhaps unexpectedly, the 2nd command is interpreted as just (1+p_k)^2.
How could one use "product" (or "sum" for that reason) and an indexed way to denote variables?
--------------
In FriCAS product denotes the formal product and in this example is
looking for an expression involving the variable k. For example:
product(sin(k),k=1..3)
But despite appearances the symbol k in p[k] in the previous command is not what FriCAS considers a variable. In FriCAS p[k] is assumed to be of type symbol, k is assumed to be a variable but variables can be coerced to be a symbol. Only variables can be assigned values but any symbol can "stand for" unknown values in expressions.
p[k]
1+p[k]
p[k]:=1
The form on the left hand side of an assignment must be a single variable,a Tuple of variables or a reference to an entry in an object supporting the setelt operation.
Understanding this distinction is somethimes important to understand why FriCAS does what it does. 1+p[k] is assumed to be a polynomial over p[k] not k.
In fact, much more complicated indexed symbols can be created in FriCAS. For example
P ==> script(p,[[a, b], [c], [d], [e]])
reduce(+,[P for a in 1..3])
Within this MathAction FriCAS wiki it is also possible to create symbols that will be displayed as superscripted or subscripted variables which can be assigned values. In fact any LaTeX symbol can be generated by using the underscore charact to "protect" special charactes such as \. For example
Q:=p_^_\alpha___\beta
1+Q
In the above the first underscore protects the second underscore which is then handled by LaTeX as the beginning of a subscript and next underscore protects the \ which begins a Greek letter, etc. FriCAS treats the entire thing as a long variable name. Note however that this only works here and not within stand alone FriCAS.
Probably what you should write is
reduce(*,[1+p[k] for k in 1..2])
Here the operation of for, creating a sequence, does not treat:
1+p[k]
as a polynomial over p[k] but the final result is a polynomial.
The same comments apply to sum() which represents a formal sum.
Alternatively, and more generally, one can use BasicOperator?
p:=operator 'p
p(1)
p(k)
product(1+p(k),k=1..2)
You can attach a display property so that the argument to p appears as a subscript
displayOfp(l:List OUTFORM):OUTFORM == subscript('p, l)
Function declaration displayOfp : List(OutputForm) -> OutputForm has
been added to workspace.
display(p,displayOfp)
Compiling function displayOfp with type List(OutputForm) -> OutputForm
product(1+p(k),k=1..2)
You can also attach an evaluation property to handle particular values of the argument
evalOfp(a:EXPR INT):EXPR INT == {one? a => 1 ; kernel(p, a)}
Function declaration evalOfp : Expression(Integer) -> Expression(
Integer) has been added to workspace.
evaluate(p,evalOfp)
Compiling function evalOfp with type Expression(Integer) -> Expression(Integer)
[p 1,p 2, p k]
product(1+p(k),k=1..2)
l:=[1,2, 4, 8, 16]
sum(l.(1+2*k),k=0..2)
There are no library operations named l Use HyperDoc Browse or issue )what op l to learn if there is any operation containing " l " in its name.
Cannot find a definition or applicable library operation named l with argument type(s) Polynomial(Integer)
Perhaps you should use "@" to indicate the required return type,or "$" to specify which version of the function you need.