开发者

Converting string to illegal/unsupported expression in Mathematica.

开发者 https://www.devze.com 2023-03-23 13:58 出处:网络
I need to input a variable, say var, into Mathematica function Series[ ] like this: Series[A^2+B^2+C^2, var]. Ser开发者_运维技巧ies[ ] has the following syntax:

I need to input a variable, say var, into Mathematica function Series[ ] like this: Series[A^2+B^2+C^2, var]. Ser开发者_运维技巧ies[ ] has the following syntax:

Series[f, {x, x_0, n}] generates a power series expansion for f about the point x=x_0 to order n.

Series[f, {x, x_0, n}, {y, y_0, m}, ...] successively finds series expansions with respect to x, then y, etc.

Because I am not always computing Series[ ] in one dimension (i.e., B and C are not always variables at each iteration), var must be properly formatted to fit the dimension demands. The caveat is that Mathematica likes lists, so any table degenerated will have a set of outer {}.

Suppose my previous code generates the following two sets of sets:

table[1]= {{A, 0, n}};

table[2]= {{A, 0, n}, {B, 0, m}}; .

My best idea is to use string manipulation (for i= 2):

string = ToString[table[i]]; .

str = StringReplacePart[string, {" ", " "}, {{1}, {StringLength[string], StringLength[string]}}]

The next step is to convert str to an expression like var and do Series[A^2 + B^2 + C^2, var] by doing var= ToExpression[str], but this returns the following error:

ToExpression::sntx: Invalid syntax in or before "{A, 0, n}, {B, 0, m}".

$Failed

Help convert str to expression propertly or suggest another way to handle this problem.


If I understood correctly, you have

table[2] = {{A, 0, n}, {B, 0, m}};

and are trying to obtain from that

Series[f[A,B],{A,0,n},{B,0,m}]

This may be done using Sequence, like so (I will use series instead of Series to keep it unevaluated so you can see what is happening):

series[f[A, B], Sequence @@ table[2]]
(*
-> series[f[A,B],{A,0,n},{B,0,m}]
*)

So for instance

table[3] = {{A, 0, 2}, {B, 0, 2}};
Series[f[A, B], Sequence @@ table[3]]

gives the right series expansion.


You can use First or Last or more generally, Part to get the List you want. For e.g.,

var = {{x, 0, 3}, {x, 0, 5}};
Series[1/(1 + x), var[[1]]]

Out[1]= 1 - x + x^2 - x^3 + O[x]^4

Series[1/(1 + x), var[[2]]]

Out[2]= 1 - x + x^2 - x^3 + x^4 - x^5 + O[x]^6

EDIT:

For multiple variables, you can use a SlotSequence (##) along with Apply (@@) like so:

Series[Sin[u + w], ##] & @@ {{u, 0, 3}, {w, 0, 3}}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号