6

I tried to do a search for this as I have to imagine it's somewhere... but I couldn't find it (probably not using the right keywords or something)

Anyway, what I want is to be able to put in a set of factors, and have the command output the distributed result. I know the polynom package can do the reverse of this with polynomials, but I am hoping to distribute out the factors.

So for example, I would input something like (x^2-1)(x+2) and the output would be x^3+2x^2-x-2

6
  • 1
    Distribution? You mean mathematical expansion of the brackets?
    – user31729
    Commented Jun 21, 2015 at 19:27
  • Yes. Even just polynomial expansion would be sufficient, although a more flexible tool would be ideal. It's a common enough thing to do I would imagine there has to be a package somewhere, but I can't seem to find it.
    – Jason
    Commented Jun 21, 2015 at 19:30
  • 7
    Well, LaTeX is no Computer algebra system
    – user31729
    Commented Jun 21, 2015 at 19:34
  • 1
    True enough, however the Polynom package allows for taking polynomials (with rational coefficients) and factoring them... which is a significantly harder computational problem. If it can do that, I would assume it could expand polynomials as it is much more straightforward.
    – Jason
    Commented Jun 21, 2015 at 19:36
  • Yes, perhaps. I've not used that package so far
    – user31729
    Commented Jun 21, 2015 at 19:38

2 Answers 2

15

The polynom package has a low level function \polyprint which prints the expanded form of a polynomial:

\documentclass{article}
\usepackage{polynom}

\begin{document}

\polyprint{(x^2-1)(x+2)}

\end{document}
3
  • Awesome, this is exactly what I was looking for! As another quick question, is there a way to use counter numbers as the numbers? the \arabic doesn't seem to work, i.e. \polyprint{(x-\arabic{counter})(x+1)} doesn't compile for "counter" being a counter that is say 1.
    – Jason
    Commented Jun 21, 2015 at 21:04
  • 1
    @Jason You should ask a separate question.
    – 11684
    Commented Jun 22, 2015 at 5:08
  • 1
    @Jason To use counters as coefficients you will need to define a new \eprintpoly command as explained here: tex.stackexchange.com/questions/81677/…
    – Alex
    Commented Jun 22, 2015 at 13:18
10

The sagetex package has this feature for polynomials and more. You need Sage installed locally or, better yet, Sagemath Cloud to run (no installation needed).

\documentclass{article}
\usepackage{sagetex}
\usepackage{amsmath}
\begin{document}
\begin{sagesilent}
x = var("x")
a = x+1
b = x^2-5*x+2
c = Integer(randint(2,7))*x^2-Integer(randint(2,7))*x+    Integer(randint(2,9))
# c is creating a random quadratic
d = sin(x)+2
e = 2^x+1
\end{sagesilent}
\noindent If we want to expand $(\sage{a})(\sage{b})$ the answer is 
$\sage{expand(a*b)}$. The expansion of $(\sage{b})(\sage{c})$
gives $\sage{b*c}$. Finally $(\sage{a})(\sage{b})(\sage{c})=\sage{expand(a*b*c)}$.

We can multiply out things which aren't polynomials, too:
\[(\sage{d})(\sage{e})=\sage{expand(d*e)}=\sage{simplify(expand(d*e))}\]
\end{document}

Which gives this output:enter image description here The Sage documentation on expand() and simplify() is here.

1
  • This seems to be a very powerful tool... although at a quick glance Sage is something like 6GB to download, which might be a little rough on a chromebook which is where I do most of my LaTeX now adays. Might be worth looking into it more though, the online one seemed to run well, but this means I need to learn Sage. Regardless, thanks.
    – Jason
    Commented Jun 21, 2015 at 21:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .