0

I'm in the process of developing simple functions that generate constants for various filters of various orders, such as Butterworth. For example, the result of Butterworth(2) would be:

s^2 + sqrt(2)*s + 1

(Darn the administrators for not choosing to support MathJax!)

I'm trying to simplify the generated coefficients. The example I'm having trouble with is a little more complicated than the example I'll present here. But the example here will get the point across.

Suppose I have the expression:

sqrt(2) * sqrt( sqrt(2) + 2 ) + 2 * sqrt( sqrt(2) + 2) )

Then using:

simplify( sqrt(2) * sqrt( sqrt(2) + 2 ) + 2 * sqrt( sqrt(2) + 2) ) )

Produces:

(sqrt(2) + 2)**(3/2)

Which is perfect for me.

But when I try something like:

simplify( 1 + sqrt(2) * sqrt( sqrt(2) + 2 ) + 2 * sqrt( sqrt(2) + 2) ) )

Then I get:

1 + sqrt(2) * sqrt( sqrt(2) + 2 ) + 2 * sqrt( sqrt(2) + 2) )

When I'd rather get:

1 + (sqrt(2) + 2)**(3/2)

Other than muddling through, using simplify() on all of the various combinations of 2 or more terms in an Add-instance to see what I get, and then prioritizing the better of them using some algorithm (as yet not well-conceived) I develop over time, my question is this:

Is there an existing function call that will recognize that:

1+sqrt(2)*sqrt(sqrt(2)+2)+2*sqrt(sqrt(2)+2)) = 1+(sqrt(2)+2)**(3/2)

I have other interests with simplification of constants that include radicals as well as rational values. But getting this far would be a big help.

[Since I'm only working with constants here (I use Poly() to gain access to the coefficients), this may be more of a Sage issue than sympy. But I'm not well-versed enough into these tools to know better. If an answer requires some explanation about the conceptual differences, feel free to inform me about it. Just FYI.]

Thanks in advance.

jonk
  • 225
  • 4
  • 11
  • I don't think this comment is worthy to be an answer but here are a few of my thoughts. [This](https://stackoverflow.com/questions/52046815/partially-factoring-an-expression-in-sympy) question is very similar and its asker has a very coincidental username. You can copy and paste images of LaTeX into SO questions and answers. If you are fairly new, some other useful functions to look at are `factor`, `cancel`, `expand`, and [others](https://docs.sympy.org/latest/modules/simplify/simplify.html). – Chris du Plessis Nov 23 '20 at 17:19
  • @Maelstrom I'm already familiar with expand and factor. They are of no help. I've yet to look at cancel or others. So I'll go try that out, quickly. (I'm in a meeting now, for a few more hours yet, though.) I'll write back when I learn more about them. But if you do test these remaining ones and if you find they solve the above shown problem, then please do write it up as an answer and show me the results. If it answers my question, it will be selected. Thanks. – jonk Nov 23 '20 at 19:04
  • @Maelstrom Funny thing. I actually thought there might be an others() definition. :) Yup. I was that dumb. But there's not, so far as I can tell. And cancel() is similarly of zero use. So I can definitely say that I'm still in the dark. However, I'm aware of cancel() now. So that's a plus. Thanks for the try. – jonk Nov 24 '20 at 02:56
  • As I say, https://stackoverflow.com/questions/52046815/partially-factoring-an-expression-in-sympy might be the best option you have. – Chris du Plessis Nov 24 '20 at 16:26
  • @Maelstrom I'll give that a thorough read and I'll see if I can make it work for me. I fear it will fail, having read over it quickly just now, but I need to spend serious time on it. So I will. I very much appreciate any suggestions like this, so thanks very much even if it doesn't pail out. I suspect I'll need to sit down and work out my own algorithm that achieves my specific goals in the end. You have my profound appreciation for the time you've already offered me. – jonk Nov 24 '20 at 16:32

0 Answers0