polynomial-math

Finding polynomial roots using Python — Possible Numpy Extension Bug

隐身守侯 提交于 2021-02-06 09:05:01
问题 I am using Numpy to obtain the roots of polynomials. Numpy provides a module 'polynomial'. My hand calc for x^2 + 5*x + 6 = 0 is x = -2 & x = -3 . (Simple) But my code shows me the wrong answer: array([-0.5 , -0.33333333]) (Inversed?) Could anyone please find the culprit in my code? Or is it simply a bug? from numpy.polynomial import Polynomial as P p = P([1, 5, 6]) p.roots() 回答1: Simply pass it in the other order, p = P([6, 5, 1]) 回答2: You could have realized this yourself if you had

Finding polynomial roots using Python — Possible Numpy Extension Bug

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-06 09:04:49
问题 I am using Numpy to obtain the roots of polynomials. Numpy provides a module 'polynomial'. My hand calc for x^2 + 5*x + 6 = 0 is x = -2 & x = -3 . (Simple) But my code shows me the wrong answer: array([-0.5 , -0.33333333]) (Inversed?) Could anyone please find the culprit in my code? Or is it simply a bug? from numpy.polynomial import Polynomial as P p = P([1, 5, 6]) p.roots() 回答1: Simply pass it in the other order, p = P([6, 5, 1]) 回答2: You could have realized this yourself if you had

How can I use gsl to calculate polynomial regression data points?

老子叫甜甜 提交于 2021-01-28 01:45:57
问题 (Disclaimer: I am terrible with math and am coming from JavaScript, so I apologize for any inaccuracies and will do my best to correct them.) The example on Rosetta Code shows how to calculate coefficients using gsl. Here is the code: polifitgsl.h: #ifndef _POLIFITGSL_H #define _POLIFITGSL_H #include <gsl/gsl_multifit.h> #include <stdbool.h> #include <math.h> bool polynomialfit(int obs, int degree, double *dx, double *dy, double *store); /* n, p */ #endif polifitgsl.cpp: #include "polifitgsl

Calculate roots of multiple polynomials

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-27 07:46:22
问题 Given a matrix A that represents polynomials in each column. How can the roots of each polynomial be calculated efficiently without loops? 回答1: Here's a comparison between 3 methods: A simple loop through all the rows, using roots on each row. A completely loopless approach, based on YBE's idea of using a block-diagonal matrix, using sparse as an intermediate A simple loop through all the rows, but this time using "inlined" code from roots . The code: %// The polynomials m = 15; n = 8; N =

Calculate roots of multiple polynomials

眉间皱痕 提交于 2021-01-27 07:44:19
问题 Given a matrix A that represents polynomials in each column. How can the roots of each polynomial be calculated efficiently without loops? 回答1: Here's a comparison between 3 methods: A simple loop through all the rows, using roots on each row. A completely loopless approach, based on YBE's idea of using a block-diagonal matrix, using sparse as an intermediate A simple loop through all the rows, but this time using "inlined" code from roots . The code: %// The polynomials m = 15; n = 8; N =

SymPy: Factorization of polynomials over symbolic roots with combined square roots

╄→гoц情女王★ 提交于 2020-06-28 03:55:32
问题 SymPy factor function can factorize over symbolic roots, e.g. : In [1]: from sympy import * In [2]: init_printing(use_latex=False) In [3]: z, a, b = symbols('z a b') In [4]: poly = expand((z - sqrt(a))*(z - sqrt(b)), z) In [5]: poly Out[5]: 2 √a⋅√b - √a⋅z - √b⋅z + z In [6]: factor(poly, z) Out[6]: (-√a + z)⋅(-√b + z) but the factorization fails if b = a : In [10]: b = a In [11]: poly = expand((z - sqrt(a))*(z - sqrt(b)), z) In [12]: poly Out[12]: 2 -2⋅√a⋅z + a + z In [13]: factor(poly, z) Out

SymPy: Factorization of polynomials over symbolic roots with combined square roots

自作多情 提交于 2020-06-28 03:55:03
问题 SymPy factor function can factorize over symbolic roots, e.g. : In [1]: from sympy import * In [2]: init_printing(use_latex=False) In [3]: z, a, b = symbols('z a b') In [4]: poly = expand((z - sqrt(a))*(z - sqrt(b)), z) In [5]: poly Out[5]: 2 √a⋅√b - √a⋅z - √b⋅z + z In [6]: factor(poly, z) Out[6]: (-√a + z)⋅(-√b + z) but the factorization fails if b = a : In [10]: b = a In [11]: poly = expand((z - sqrt(a))*(z - sqrt(b)), z) In [12]: poly Out[12]: 2 -2⋅√a⋅z + a + z In [13]: factor(poly, z) Out

Full factorization of polynomials over complexes with SymPy

亡梦爱人 提交于 2020-06-17 09:33:12
问题 I want to fully factorize a polynom, thus factorize it over complexes. SymPy provide factor to do it, but I’m very surprised that factorization is done only over integer roots, e.g. : >>> from sympy import * >>> z = symbols('z') >>> factor(z**2 - 1, z) (z - 1)*(z + 1) >>> factor(z**2 + 1, z) z**2 + 1 or >>> factor(2*z - 1, z) 2*z - 1 >>> factor(2*z - 1, z, extension=[Integer(1)/2]) 2*(z - 1/2) An answered question already exists : Factor to complex roots using sympy, and the solution given by

Setting up array of complex coefficients, avoiding the leading zero's

半世苍凉 提交于 2020-03-25 04:22:41
问题 I have created a class for complex numbers: public class Complex { private double x; //Real part x of the complex number x+iy. private double y; //Imaginary part y of the complex number x+iy. public Complex(double x, double y) { //Constructor: Initializes x, y. this.x=x; this.y=y; } public Complex(double x) { //Real constructor - initialises with a real number. this(x, 0.0); } public Complex() { //Default constructor; initialiase x and y to zero. this(0.0, 0.0); } } What I would like to do is

How can I create functions that handle polynomials?

痴心易碎 提交于 2020-01-23 07:03:52
问题 I have these problems about polynomials and I've spent about 4 hours on this, but I just can't get it. I'm new to Python and programming and I've tried working it out on paper, but I just don't know. Write and test a Python function negate(p) that negates the polynomial represented by the list of its coeffeicients p and returns a new polynomial (represented as a list). In other words, write a function that makes the list of numbers negative. Write a Python function eval_polynomial(p, x) that