Tags
 IOS SQL HTML C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6

# How to find coefficient of the line equation?

By : Sacha Vannattan
Date : July 31 2020, 08:00 PM
I wish this helpful for you I case you want to parse a string with numbers for a, b and c, then the following python example shows you how to do this:
code :
``````import re

p = re.compile('([\d*\.\d+|\d+]+)x\+([\d*\.\d+|\d+]+)y\+([\d*\.\d+|\d+]+)')
m = p.match("1.2x+3y+4.5")

print("a=%s" % m.group(1))
print("b=%s" % m.group(2))
print("c=%s" % m.group(3))
``````
``````e = "1.2x-3y+4.5"

p=['']
for c in e: # loop through all characters
if c in ['.', '-'] or c.isdigit(): # if it's dot, minus or digit append it to the last parameter
p[len(p) - 1] += c
else: # otherwise create a new parameter
if len(p[len(p) - 1]) != 0: # when there isn't one yet
p.append('') # append new parameter

print("a=%s" % p[0])
print("b=%s" % p[1])
print("c=%s" % p[2])
``````

Share :

## 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:
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*(6x-3) = 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);

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());
}

double constant0 = jep.getValue();
double value0 = jep.getValue();

jep.parseExpression(s1);
if (jep.hasError())
{
throw new IllegalArgumentException(jep.getErrorInfo());
}

double constant1 = jep.getValue();
double value1 = jep.getValue();

constant = constant0 - constant1;
coefficient = (value0 - constant0) - (value1-constant1);
}

public double getCoefficient()
{
return coefficient;
}

public double getConstant()
{
return constant;
}

}
``````
``````Input: 3x = 5
Coefficient: 3.0
Constant   : -5.0

Input: 3x +2*(6x-3) = 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])

# x-coordinates of intersections
xi = (offsets - offsets.T) / (slopes.T - slopes)

# y-coordinates 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^3-18x^2-12x+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^3-18x^2-12x+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