coefficient grouping: coefficient equation from a longer expression
By : Srikanth Patil
Date : March 29 2020, 07:55 AM
wish helps you collect is the right tool for this: Example from the above link: code :
>>> collect(a*x**2 + b*x**2 + a*x  b*x + c, x)
c + x**2*(a + b) + x*(a  b)
col = collect(Det, [X1, Y1], evaluate=False)
A = col[X1]
B = col[Y1]
C = col[S.One]

Parse Coefficient of an Linear equation
By : Brook Zhu
Date : March 29 2020, 07:55 AM
I wish did fix the issue. As already suggested by my comment: This may be arbitrarily complicated, depending on what exactly this parser should support. There are several potentially very complex and challenging tasks involved here. The first one is parsing itself. Although it's well understood and there are supporting tools for writing parsers and all, it would be tedious (and would involve some effort) to write a robust, reliable parser for these expressions from scratch. code :
import org.nfunk.jep.JEP;
public class LinearEquationParser
{
private double coefficient;
private double constant;
public static void main(String[] args)
{
runTest("3x = 5");
runTest("3x +2*(6x3) = 2 4x");
runTest("3x + 2*(6x sin(3))=cos(2)4*x*log(tan(43))");
}
private static void runTest(String s)
{
System.out.println("Input: "+s);
LinearEquationParser p = new LinearEquationParser();
p.process(s);
System.out.println("Coefficient: "+p.getCoefficient());
System.out.println("Constant : "+p.getConstant());
System.out.println();
}
public void process(String s)
{
JEP jep = new JEP();
jep.setImplicitMul(true);
jep.addStandardFunctions();
jep.addStandardConstants();
jep.addVariable("x", 0.0);
String s0 = s.substring(0, s.indexOf("="));
String s1 = s.substring(s.indexOf("=")+1, s.length());
jep.parseExpression(s0);
if (jep.hasError())
{
throw new IllegalArgumentException(jep.getErrorInfo());
}
jep.addVariable("x", 0.0);
double constant0 = jep.getValue();
jep.addVariable("x", 1.0);
double value0 = jep.getValue();
jep.parseExpression(s1);
if (jep.hasError())
{
throw new IllegalArgumentException(jep.getErrorInfo());
}
jep.addVariable("x", 0.0);
double constant1 = jep.getValue();
jep.addVariable("x", 1.0);
double value1 = jep.getValue();
constant = constant0  constant1;
coefficient = (value0  constant0)  (value1constant1);
}
public double getCoefficient()
{
return coefficient;
}
public double getConstant()
{
return constant;
}
}
Input: 3x = 5
Coefficient: 3.0
Constant : 5.0
Input: 3x +2*(6x3) = 2 4x
Coefficient: 19.0
Constant : 8.0
Input: 3x + 2*(6x sin(3))=cos(2)4*x*log(tan(43))
Coefficient: 15.7024963786418
Constant : 0.13390682042740798

How to find line intersections with single line equation array in a pythonic way
By : Nikhil Mittal
Date : March 29 2020, 07:55 AM
Does that help The equation for the intersection of two lines y1 = a1*x + b1 and y2 = a2*x + b2 is x = (b2  b1) / (a1  a2). By making use of broadcasting it is easy to compute all intersections between any number of lines: code :
import numpy as np
# lines of the form y = a * x + b
# with lines = [[a0, b0], ..., [aN, bN]]
lines = np.array([[1, 0], [0.5, 0], [1, 3], [1, 2]])
slopes = lines[:, 0] # array with slopes (shape [N])
slopes = slopes[:, np.newaxis] # column vector (shape [N, 1])
offsets = lines[:, 1] # array with offsets (shape [N])
offsets = offsets[:, np.newaxis] # column vector (shape [N, 1])
# xcoordinates of intersections
xi = (offsets  offsets.T) / (slopes.T  slopes)
# ycoordinates of intersections
yi = xi * slopes + offsets
#visualize the result
import matplotlib.pyplot as plt
for l in lines:
x = np.array([5, 5])
plt.plot(x, x * l[0] + l[1], label='{}x + {}'.format(l[0], l[1]))
for x, y in zip(xi, yi) :
plt.plot(x, y, 'ko')
plt.legend()
plt.show()

How do i regex coefficient of cubic equation?
By : user3477795
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I am having a cubic equation 2x^318x^212x+112=0 . I want to regex out the coefficients of the equation ..so that i am left with... , If the coefficient are integers, you can do: code :
s = '2x^318x^212x+112=0'
import re
a, b, c, d = map(int, re.findall(r'(?<!\^=)([\d]+)', s))
print(a)
print(b)
print(c)
print(d)
2
18
12
112

solve equation using a list as coefficient
By : olddonkey
Date : March 29 2020, 07:55 AM
To fix this issue I am trying to solve this equation : , This will do it

