Mathematica: Splitting a function into a list of terms

In summary, the speakers discuss a method for splitting an expression into a list of terms. The first speaker shares their code for achieving this, using StringSplit and StringTake functions. The second speaker suggests using Apply to make the Plus operator become a List. They also provide a link for further discussion and alternative options.
  • #1
TenFold
2
0
Hi,

I need to split an expression into a list of terms, i.e. an expression like:
a+b-c-d+...
into:
{a,b,-c,-d,...}.

Can you help? Thanks in advance.
 
Physics news on Phys.org
  • #2
Maybe it's not too slick, but I came up with this:

myString = "a+b-c-d";
If[StringTake[myString, 1] != "-", myString = "+" <> myString,];
list = StringSplit[myString, {"+" -> "1", "-" -> "-1"}];
listout = Range[Length
  • /2];
    For[i = 1, i <= Length
    • /2, i++,
      listout[] = ToExpression[list[[2 i - 1]]]*ToExpression[list[[2 i]]]];
      listout

      which gives {a,b,-c,-d}.
 
  • #3
String methods are probably not the best for this problem.

If you look at the FullForm[a+b-c-d] you see that it's
Plus[a, b, Times[-1, c], Times[-1, d]]
So all you need to do is make the Plus become a List.
This can be done using Apply, which has the short hand of @@.
So, the following should return True
List@@(a+b-c-d) == {a,b,-c,-d}

See http://stackoverflow.com/q/7697614/421225 for more discussion and different options.
 

Related to Mathematica: Splitting a function into a list of terms

1. What is the purpose of splitting a function into a list of terms?

Splitting a function into a list of terms allows for easier manipulation and analysis of the individual components of the function. It can also help identify patterns and relationships between the terms.

2. How do I split a function into a list of terms in Mathematica?

To split a function into a list of terms in Mathematica, you can use the function CoefficientList. This function takes in the function as its first argument and the variable as its second argument. It will return a list of coefficients for each term in the function.

3. Can I specify the order of the terms in the resulting list?

Yes, you can specify the order of the terms in the resulting list by using the option ExponentOrder in the CoefficientList function. This option allows you to specify the order of the terms based on a specific variable or list of variables.

4. What if my function has multiple variables?

If your function has multiple variables, you can use the CoefficientArrays function instead. This function will return a list of arrays, each representing the coefficients for a different variable in the function.

5. Are there any other Mathematica functions that can help with splitting a function into terms?

Yes, there are several other functions in Mathematica that can help with splitting a function into terms. These include MonomialList, PolynomialReduce, and GatherBy. These functions each have their own specific purposes and can be useful for different types of functions.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
486
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
313
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
331
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
Back
Top