天道酬勤,学无止境

polynomials

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

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[13]: 2 -2⋅√a⋅z + a + z Thus, the factorization fails if the identity sqrt(a) * sqrt(a) = a is applied

2021-09-08 09:17:29    分类:问答    python   math   sympy   polynomials   polynomial-math

I would like to print superscript and subscript with printf, like x¹?

I want to print out a polynomial expression in c but i don't know print x to the power of a number with printf

2021-09-04 07:54:48    分类:问答    c   unicode   polynomials   subscript   superscript

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

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 create a function Polynomial, which would take an array of coefficients, and filter it so that if for

2021-09-04 07:13:29    分类:问答    java   math   numbers   polynomials   polynomial-math

Find tangent points on a curve from a user-given point outside the curve

I am trying to find the tangent lines from a given point outside a closed curve (not on the curve). The curve is defined as 2D x and y coordinates of points,shaped like an irregular ellipse for example. If given a point by the user: (x0,y0) = (-30,80), how can I know the tangent points (obviously closest point among discrete points from the smooth curve) on curve (i.e. tangent lines from (x0,y0) to curve)?

2021-09-03 23:21:25    分类:问答    matlab   geometry   curve-fitting   polynomials

Sympy:删除多项式中的高阶项(Sympy: Drop higher order terms in polynomial)

问题 使用 Sympy,假设我们有一个表达式 f,它是符号“x”(可能还有其他符号)的多项式。 我想知道是否有一种有效的方法可以删除大于某个整数 n 的 f 阶中的所有项。 作为一种特殊情况,我有一个非常复杂的函数,但我只想将项保持在 x 中的二阶。 这样做的有效方法是什么? 显而易见的,不是非常有效的方法是对于每个小于 n 的 m,取 m 个导数并将 x 设置为 0 以获得 x^m 的系数。 我们以这种方式获得每个系数,然后重建多项式。 但采取衍生品并不是最有效的方式。 回答1 一种简单的方法是将O(x**n)到表达式中,例如 In [23]: x + x**2 + x**4 + x**10 + O(x**3) Out[23]: 2 ⎛ 3⎞ x + x + O⎝x ⎠ 如果您想稍后删除它,请使用removeO方法 In [24]: (x + x**2 + x**4 + x**10 + O(x**3)).removeO() Out[24]: 2 x + x 您还可以使用series对表达式进行级数扩展。 这里的区别在于如果非多项式项出现在表达式中时的行为: In [25]: x + sin(x) + O(x**3) Out[25]: ⎛ 3⎞ sin(x) + x + O⎝x ⎠ In [26]: (x + sin(x)).series(x, 0, 3) Out[26]: ⎛ 3

2021-09-01 07:25:23    分类:技术分享    python   sympy   symbolic-math   computer-algebra-systems   polynomials

在java中使用链表进行多项式加法(polynomial addition using linked list in java)

问题 这是我使用链表实现两个多项式相加的实现。 例如,如果我想添加 3x^2+5^x+3 和 4x^3+5x+2 首先,我检查两个多项式中是否有相似的指数,如果有,我将它们的系数相加,然后将指数附加到一个字符串中。 添加类似的指数然后使用字符串后,我将两个多项式中的其余部分添加到最终结果中。 public class Node2{ int coef; int exp; Node2 next; Node2(int c,int e,Node2 n){ coef=c; exp=e; next=n; } Node2(int c,int e){ coef=c; exp=e; } } public class LinkedPoly{ static String exponent=""; Node2 head; Node2 current; LinkedPoly(){ head=null; } public void createList(int c,int e){ head=new Node2(c,e,head); } public static LinkedPoly add(LinkedPoly list1,LinkedPoly list2){ LinkedPoly addList=new LinkedPoly(); Node2 temp1=list1.head; Node2 temp3

2021-08-31 21:16:38    分类:技术分享    java   linked-list   polynomials

Extract coefficients and corresponding monomials from a given polynomial in SymPy

Given a symbolic multivariate polynomial P, I need to extract both its coefficients and corresponding monomials as lists: def poly_decomp(P): .... return coeffs, monoms such that P is the dot product of coefficients and monomials, e.g., if P(x,y) = ax**2 + bxy + cy**2 then we should get coeffs = [a, b, c] and monoms = [x**2, x*y, y**2]. Getting the coefficients is easy since the function is built in coeffs = P.coeffs(). However, I'm having trouble getting the monomials. Here the build in function returns a list of exponents, e.g., in the example above we would get P.monoms() = [(2,0),(1,1),(0

2021-08-30 06:51:14    分类:问答    python   python-3.x   sympy   polynomials

Creating a python lmfit Model with arbitrary number of parameters

Is there a way to construct a an lmfit Model based on a function with an arbitrary number of dependent variables? For example: from lmfit import Model def my_poly(x, *params): func = 0 for i in range(len(params)): func+= params[i]*z**i return func #note: below does not work my_model = Model(my_poly, independent_vars = ['x'], param_names = ['A','B','C']) Something similar to the above would be wonderful if I am interested in a polynomial series and want to test the performance as the series grows or shrinks.

2021-08-30 00:47:16    分类:问答    python   curve-fitting   polynomials   lmfit

Find the coefficients of the polynomial given its roots

I am trying to write an algorithm which will find a(0),..., a(n-1), given the values of n, x_1, ..., x_n, a(n), such that: a(n)*p^n + a(n-1)*p^(n-1) + ... + a(1)*p + a(0) = a(n)(p-x_1)(p-x_2)...(p-x_n) for all real p. After multiplying a(n)(p-x_1)(p-x_2) I've thought of using Viete's formulas to find the coefficients. But it turns out writing the code down isn't as obvious as I expected. I want to use only the basics in my code - that is loops, if-s addition and multiplication - no ready/ complex functions. Here are the formulas: First, I would like to emphasise that I only need a pseudocode

2021-08-12 07:09:59    分类:问答    algorithm   loops   for-loop   polynomial-math   polynomials

获取向量幂的有效方法(efficient way to take powers of a vector)

问题 我写了一个代码,在数值上使用了勒让德多项式直到某个高 n 阶。 例如: .... case 8 p = (6435*x.^8-12012*x.^6+6930*x.^4-1260*x.^2+35)/128; return case 9 ... 如果向量x很长,这可能会变慢。 我看到x.^4和x.*x.*x.*x之间存在性能差异,并认为我可以用它来改进我的代码。 我使用了timeit并发现它用于: x=linspace(0,10,1e6); f1= @() power(x,4) f2= @() x.4; f3= @() x.^2.^2 f4= @() x.*x.*x.*x f4比其他的快2 倍。 但是,当我去x.^6 , (x.*x.*x).^2和x.*x.*x.*x.*x.*x之间几乎没有区别(而所有其他选项都是慢点)。 有没有办法告诉我们什么是获取矢量幂的最有效方法? 你能解释一下为什么性能会有如此大的差异吗? 回答1 这不完全是您问题的答案,但它可能会解决您的问题: x2 = x.*x; % or x.^2 or power(x,2), whichever is most efficient p = ((((6435*x2-12012)*x2+6930)*x2-1260)*x2+35)/128 通过这种方式,您只需执行一次幂,并且仅使用指数 2

2021-08-12 00:57:45    分类:技术分享    arrays   matlab   polynomials