# Solvers Python or Apache

**URL:** <https://forum.inductiveautomation.com/t/solvers-python-or-apache/56746>\
**Category:** General Discussion\
**Tags:** ignition79\
**Created:** [March 2, 2022, 9:14pm UTC](https://forum.inductiveautomation.com/t/solvers-python-or-apache/56746 "2022-03-02T21:14:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pcurran](https://avatars.discourse-cdn.com/v4/letter/p/7c8e57/32.png) [@pcurran](https://forum.inductiveautomation.com/u/pcurran)\
**Post date:** [March 2, 2022, 9:14pm UTC](https://forum.inductiveautomation.com/t/solvers-python-or-apache/56746/1 "2022-03-02T21:14:00Z")

</div>

Trying to solve a 3rd degree polynomial and feed those coefficients into a solver to determine x given y (at eft 6 hrs what density should I expect). Im using 7.9 so is there any good solvers that I could use from Apache Commons or a good way to roll my own?

```auto
from org.apache.commons.math3.fitting import PolynomialCurveFitter as pcf
from org.apache.commons.math3.fitting import WeightedObservedPoints as wop

eft = [0.0, 7, 15, 23]
dcw = [0.583,0.72, 2.82, 7.29]
fitter = pcf.create(2)
tstPoints = wop()

for i in range(4):
   time = eft[i]
   density = dcw[i]
   tstPoints.add(time, density)

coeff = fitter.fit(tstPoints.toList())
for i in range(len(coeff)):
    print coeff[i]

```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 3, 2022, 11:25am UTC](https://forum.inductiveautomation.com/t/solvers-python-or-apache/56746/2 "2022-03-03T11:25:52Z")

</div>

I wrote this before we access to Apache math. May be useful for you. The polyfit() function is what I think you're looking for.

> [@Math Extensions for Ignition](https://forum.inductiveautomation.com/t/math-extensions-for-ignition/3106):
>
> EDIT 2017-07-13: Things have changed enough that this first post is getting revamped. EDIT 2019-08-08: Re-uploaded due to broken link Some useful math functions just aren’t available through scripting. This is an attempt to rectify some of it. Functions: sum mean rectangular-to-polar conversions polar-to-rectangular conversions simple linear regressions polynomial regression greatest-common-denominator least common multiple standard deviation (population and sample) decimal to BCD decimal t…

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [March 3, 2022, 1:58pm UTC](https://forum.inductiveautomation.com/t/solvers-python-or-apache/56746/3 "2022-03-03T13:58:14Z")

</div>

I don’t know of any built in ones off hand.

@JordanCClark 's library seems to have the functionality you need (and up to 5th degree polynomials it looks like).

But if finding roots of a 3rd degree polynomial is all you need, there is a formula for it you could hardcode by hand. The hard part I imagine would be handling the potential imaginary numbers that can arise [The Cubic Formula](https://math.vanderbilt.edu/schectex/courses/cubic/)

If you’re not comfortable using someone else’s library and or handling the imaginary numbers, you can always try implementing your own version of Newton’s method or some other numerical approximation method for finding roots for which you can find the process online.

---

<div class="post-metadata">

**Author:** ![pcurran](https://avatars.discourse-cdn.com/v4/letter/p/7c8e57/32.png) [@pcurran](https://forum.inductiveautomation.com/u/pcurran)\
**Post date:** [March 4, 2022, 7:12pm UTC](https://forum.inductiveautomation.com/t/solvers-python-or-apache/56746/4 "2022-03-04T19:12:46Z")

</div>

Thanks @JordanCClark and @bkarabinchak.psi for the libraries and recommendations. I figured since I was using the Apache Commons library to get the nth degree polynomial I could then put that into a solver from the same library to get the roots maybe something like these [solvers]. ([LaguerreSolver (Apache Commons Math 3.3 API)](https://commons.apache.org/proper/commons-math/javadocs/api-3.3/org/apache/commons/math3/analysis/solvers/LaguerreSolver.html))

Since we converting an Excel sheet to Ignition were just using the goal seek option in Excel to solve for x given y using the coefficients. Is there a good way to implement this in Ignition using some existing library?

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [March 4, 2022, 7:33pm UTC](https://forum.inductiveautomation.com/t/solvers-python-or-apache/56746/5 "2022-03-04T19:33:40Z")

</div>

I haven’t verified this personally but this post in stackoverflow -

> <https://stackoverflow.com/questions/47517741/solving-polynomial-equation-in-java>

uses the brent Method in java for finding roots. Read the entire post as there’s some caveats - only finds roots within your interval range, and only that are more than an interval size apart (in his example, more than .01 apart).

You can try replicating his code. You can start off with

```auto
import org.apache.commons.math3.analysis.UnivariateFunction
import org.apache.commons.math3.analysis.solvers.BrentSolver

```

and go from there.

I don’t know how excel’s goal seek works behind the scenes but you are just trying to find roots essentially so any root finding algorithm should fit your needs.

---

<div class="post-metadata">

**Author:** ![pcurran](https://avatars.discourse-cdn.com/v4/letter/p/7c8e57/32.png) [@pcurran](https://forum.inductiveautomation.com/u/pcurran)\
**Post date:** [March 4, 2022, 8:51pm UTC](https://forum.inductiveautomation.com/t/solvers-python-or-apache/56746/6 "2022-03-04T20:51:07Z")

</div>

Thats a great example. How would you instantiate the UnivariateFunction from java?

```auto
UnivariateFunction f = new UnivariateFunction() {

            @Override
            public double value(double x) {
                return 103*Math.pow(x, 5.0)-5*Math.pow(x, 4.0)-5*Math.pow(x, 3.0)-5*Math.pow(x, 2.0)-5*x - 105;
            }
        };

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [March 4, 2022, 9:41pm UTC](https://forum.inductiveautomation.com/t/solvers-python-or-apache/56746/7 "2022-03-04T21:41:33Z")

</div>

You would just make a Python subclass, e.g.:

```auto
from org.apache.commons.math3.analysis import UnivariateFunction

class myFunction(UnivariateFunction):
	def value(self, x):
		return 0.0 # do math here

```
