1 Star 0 Fork 0

zhouxs1023/eigenmath_pratt

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
factorpoly.cpp 11.18 KB
一键复制 编辑 原始数据 按行查看 历史
Calin Barbat 提交于 2018-02-24 10:52 . Initial commit.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
// Factor a polynomial
#include "stdafx.h"
#include "defs.h"
static void rationalize_coefficients(int);
static int get_factor(void);
static void evalpoly(void);
static void yydivpoly(void);
static int expo;
static U **polycoeff;
#define POLY p1
#define X p2
#define Z p3
#define A p4
#define B p5
#define Q p6
#define RESULT p7
#define FACTOR p8
void
factorpoly(void)
{
save();
p2 = pop();
p1 = pop();
if (!find(p1, p2)) {
push(p1);
restore();
return;
}
if (!ispoly(p1, p2)) {
push(p1);
restore();
return;
}
if (!issymbol(p2)) {
push(p1);
restore();
return;
}
push(p1);
push(p2);
yyfactorpoly();
restore();
}
//-----------------------------------------------------------------------------
//
// Input: tos-2 true polynomial
//
// tos-1 free variable
//
// Output: factored polynomial on stack
//
//-----------------------------------------------------------------------------
void
yyfactorpoly(void)
{
int h, i;
save();
X = pop();
POLY = pop();
h = tos;
if (isfloating(POLY))
stop("floating point numbers in polynomial");
polycoeff = stack + tos;
push(POLY);
push(X);
expo = coeff() - 1;
rationalize_coefficients(h);
// for univariate polynomials we could do expo > 1
while (expo > 0) {
if (iszero(polycoeff[0])) {
push_integer(1);
A = pop();
push_integer(0);
B = pop();
} else if (get_factor() == 0) {
if (verbosing)
printf("no factor found\n");
break;
}
push(A);
push(X);
multiply();
push(B);
add();
FACTOR = pop();
if (verbosing) {
printf("success\nFACTOR=");
print(FACTOR);
printf("\n");
}
// factor out negative sign (not req'd because A > 1)
#if 0
if (isnegativeterm(A)) {
push(FACTOR);
negate();
FACTOR = pop();
push(RESULT);
negate_noexpand();
RESULT = pop();
}
#endif
push(RESULT);
push(FACTOR);
multiply_noexpand();
RESULT = pop();
yydivpoly();
while (expo && iszero(polycoeff[expo]))
expo--;
}
// unfactored polynomial
push(zero);
for (i = 0; i <= expo; i++) {
push(polycoeff[i]);
push(X);
push_integer(i);
power();
multiply();
add();
}
POLY = pop();
if (verbosing) {
printf("POLY=");
print(POLY);
printf("\n");
}
// factor out negative sign
if (expo > 0 && isnegativeterm(polycoeff[expo])) {
push(POLY);
negate();
POLY = pop();
push(RESULT);
negate_noexpand();
RESULT = pop();
}
push(RESULT);
push(POLY);
multiply_noexpand();
RESULT = pop();
if (verbosing) {
printf("RESULT=");
print(RESULT);
printf("\n");
}
stack[h] = RESULT;
tos = h + 1;
restore();
}
static void
rationalize_coefficients(int h)
{
int i;
// LCM of all polynomial coefficients
RESULT = one;
for (i = h; i < tos; i++) {
push(stack[i]);
denominator();
push(RESULT);
lcm();
RESULT = pop();
}
// multiply each coefficient by RESULT
for (i = h; i < tos; i++) {
push(RESULT);
push(stack[i]);
multiply();
stack[i] = pop();
}
// reciprocate RESULT
push(RESULT);
reciprocate();
RESULT = pop();
}
static int
get_factor(void)
{
int i, j, h;
int a0, an, na0, nan;
if (verbosing) {
push(zero);
for (i = 0; i <= expo; i++) {
push(polycoeff[i]);
push(X);
push_integer(i);
power();
multiply();
add();
}
POLY = pop();
printf("POLY=");
print(POLY);
printf("\n");
}
h = tos;
an = tos;
push(polycoeff[expo]);
divisors_onstack();
nan = tos - an;
a0 = tos;
push(polycoeff[0]);
divisors_onstack();
na0 = tos - a0;
if (verbosing) {
printf("divisors of base term");
for (i = 0; i < na0; i++) {
printf(", ");
print(stack[a0 + i]);
}
printf("\n");
printf("divisors of leading term");
for (i = 0; i < nan; i++) {
printf(", ");
print(stack[an + i]);
}
printf("\n");
}
// try roots
for (i = 0; i < nan; i++) {
for (j = 0; j < na0; j++) {
A = stack[an + i];
B = stack[a0 + j];
push(B);
push(A);
divide();
negate();
Z = pop();
evalpoly();
if (verbosing) {
printf("try A=");
print(A);
printf(", B=");
print(B);
printf(", root ");
print(X);
printf("=-B/A=");
print(Z);
printf(", POLY(");
print(Z);
printf(")=");
print(Q);
printf("\n");
}
if (iszero(Q)) {
tos = h;
return 1;
}
push(B);
negate();
B = pop();
push(Z);
negate();
Z = pop();
evalpoly();
if (verbosing) {
printf("try A=");
print(A);
printf(", B=");
print(B);
printf(", root ");
print(X);
printf("=-B/A=");
print(Z);
printf(", POLY(");
print(Z);
printf(")=");
print(Q);
printf("\n");
}
if (iszero(Q)) {
tos = h;
return 1;
}
}
}
tos = h;
return 0;
}
//-----------------------------------------------------------------------------
//
// Divide a polynomial by Ax+B
//
// Input: polycoeff Dividend coefficients
//
// expo Degree of dividend
//
// A As above
//
// B As above
//
// Output: polycoeff Contains quotient coefficients
//
//-----------------------------------------------------------------------------
static void
yydivpoly(void)
{
int i;
Q = zero;
for (i = expo; i > 0; i--) {
push(polycoeff[i]);
polycoeff[i] = Q;
push(A);
divide();
Q = pop();
push(polycoeff[i - 1]);
push(Q);
push(B);
multiply();
subtract();
polycoeff[i - 1] = pop();
}
polycoeff[0] = Q;
}
static void
evalpoly(void)
{
int i;
push(zero);
for (i = expo; i >= 0; i--) {
push(Z);
multiply();
push(polycoeff[i]);
add();
}
Q = pop();
}
#if SELFTEST
static const char *s[] = {
"bake=0",
"",
"factor((x+1)*(x+2)*(x+3),x)",
"(1+x)*(2+x)*(3+x)",
"factor((x+a)*(x^2+x+1),x)",
"(1+x+x^2)*(a+x)",
"factor(x*(x+1)*(x+2),x)",
"x*(1+x)*(2+x)",
// "factor((x+1)*(x+2)*(y+3)*(y+4),x,y)",
// "(1+x)*(2+x)*(3+y)*(4+y)",
// "factor((x+1)*(x+2)*(y+3)*(y+4),(x,y))",
// "(1+x)*(2+x)*(3+y)*(4+y)",
// "factor((x+1)*(x+2)*(y+3)*(y+4))",
// "(1+x)*(2+x)*(3+y)*(4+y)",
"factor((-2*x+3)*(x+4),x)",
"-(-3+2*x)*(4+x)",
"(-2*x+3)*(x+4)+(-3+2*x)*(4+x)",
"0",
// make sure sign of remaining poly is factored
"factor((x+1)*(-x^2+x+1),x)",
"-(-1-x+x^2)*(1+x)",
// sign handling
//++++++
"factor((x+1/2)*(+x+1/3)*(+x+1/4),x)",
"1/24*(1+2*x)*(1+3*x)*(1+4*x)",
"(x+1/2)*(+x+1/3)*(+x+1/4)-1/24*(1+2*x)*(1+3*x)*(1+4*x)",
"0",
//+++++-
"factor((x+1/2)*(+x+1/3)*(+x-1/4),x)",
"1/24*(-1+4*x)*(1+2*x)*(1+3*x)",
"(x+1/2)*(+x+1/3)*(+x-1/4)-1/24*(-1+4*x)*(1+2*x)*(1+3*x)",
"0",
//++++-+
"factor((x+1/2)*(+x+1/3)*(-x+1/4),x)",
"-1/24*(-1+4*x)*(1+2*x)*(1+3*x)",
"(x+1/2)*(+x+1/3)*(-x+1/4)+1/24*(-1+4*x)*(1+2*x)*(1+3*x)",
"0",
//++++--
"factor((x+1/2)*(+x+1/3)*(-x-1/4),x)",
"-1/24*(1+2*x)*(1+3*x)*(1+4*x)",
"(x+1/2)*(+x+1/3)*(-x-1/4)+1/24*(1+2*x)*(1+3*x)*(1+4*x)",
"0",
//+++-++
"factor((x+1/2)*(+x-1/3)*(+x+1/4),x)",
"1/24*(-1+3*x)*(1+2*x)*(1+4*x)",
"(x+1/2)*(+x-1/3)*(+x+1/4)-1/24*(-1+3*x)*(1+2*x)*(1+4*x)",
"0",
//+++-+-
"factor((x+1/2)*(+x-1/3)*(+x-1/4),x)",
"1/24*(-1+3*x)*(-1+4*x)*(1+2*x)",
"(x+1/2)*(+x-1/3)*(+x-1/4)-1/24*(-1+3*x)*(-1+4*x)*(1+2*x)",
"0",
//+++--+
"factor((x+1/2)*(+x-1/3)*(-x+1/4),x)",
"-1/24*(-1+3*x)*(-1+4*x)*(1+2*x)",
"(x+1/2)*(+x-1/3)*(-x+1/4)+1/24*(-1+3*x)*(-1+4*x)*(1+2*x)",
"0",
//+++---
"factor((x+1/2)*(+x-1/3)*(-x-1/4),x)",
"-1/24*(-1+3*x)*(1+2*x)*(1+4*x)",
"(x+1/2)*(+x-1/3)*(-x-1/4)+1/24*(-1+3*x)*(1+2*x)*(1+4*x)",
"0",
//++-+++
"factor((x+1/2)*(-x+1/3)*(+x+1/4),x)",
"-1/24*(-1+3*x)*(1+2*x)*(1+4*x)",
"(x+1/2)*(-x+1/3)*(+x+1/4)+1/24*(-1+3*x)*(1+2*x)*(1+4*x)",
"0",
//++-++-
"factor((x+1/2)*(-x+1/3)*(+x-1/4),x)",
"-1/24*(-1+3*x)*(-1+4*x)*(1+2*x)",
"(x+1/2)*(-x+1/3)*(+x-1/4)+1/24*(-1+3*x)*(-1+4*x)*(1+2*x)",
"0",
//++-+-+
"factor((x+1/2)*(-x+1/3)*(-x+1/4),x)",
"1/24*(-1+3*x)*(-1+4*x)*(1+2*x)",
"(x+1/2)*(-x+1/3)*(-x+1/4)-1/24*(-1+3*x)*(-1+4*x)*(1+2*x)",
"0",
//++-+--
"factor((x+1/2)*(-x+1/3)*(-x-1/4),x)",
"1/24*(-1+3*x)*(1+2*x)*(1+4*x)",
"(x+1/2)*(-x+1/3)*(-x-1/4)-1/24*(-1+3*x)*(1+2*x)*(1+4*x)",
"0",
//++--++
"factor((x+1/2)*(-x-1/3)*(+x+1/4),x)",
"-1/24*(1+2*x)*(1+3*x)*(1+4*x)",
"(x+1/2)*(-x-1/3)*(+x+1/4)+1/24*(1+2*x)*(1+3*x)*(1+4*x)",
"0",
//++--+-
"factor((x+1/2)*(-x-1/3)*(+x-1/4),x)",
"-1/24*(-1+4*x)*(1+2*x)*(1+3*x)",
"(x+1/2)*(-x-1/3)*(+x-1/4)+1/24*(-1+4*x)*(1+2*x)*(1+3*x)",
"0",
//++---+
"factor((x+1/2)*(-x-1/3)*(-x+1/4),x)",
"1/24*(-1+4*x)*(1+2*x)*(1+3*x)",
"(x+1/2)*(-x-1/3)*(-x+1/4)-1/24*(-1+4*x)*(1+2*x)*(1+3*x)",
"0",
//++----
"factor((x+1/2)*(-x-1/3)*(-x-1/4),x)",
"1/24*(1+2*x)*(1+3*x)*(1+4*x)",
"(x+1/2)*(-x-1/3)*(-x-1/4)-1/24*(1+2*x)*(1+3*x)*(1+4*x)",
"0",
//++++++
"factor((+x+a)*(+x+b)*(+x+c),x)",
"(a+x)*(b+x)*(c+x)",
"(a+x)*(b+x)*(c+x)-(a+x)*(b+x)*(c+x)",
"0",
//+++++-
"factor((+x+a)*(+x+b)*(+x-c),x)",
"(a+x)*(b+x)*(-c+x)",
"(+x+a)*(+x+b)*(+x-c)-(a+x)*(b+x)*(-c+x)",
"0",
//++++-+
"factor((+x+a)*(+x+b)*(-x+c),x)",
"-(a+x)*(b+x)*(-c+x)",
"(+x+a)*(+x+b)*(-x+c)+(a+x)*(b+x)*(-c+x)",
"0",
//++++--
"factor((+x+a)*(+x+b)*(-x-c),x)",
"-(a+x)*(b+x)*(c+x)",
"(+x+a)*(+x+b)*(-x-c)+(a+x)*(b+x)*(c+x)",
"0",
//++++
"factor((+a*x+b)*(+c*x+d),x)",
"(b+a*x)*(d+c*x)",
"(+a*x+b)*(+c*x+d)-(b+a*x)*(d+c*x)",
"0",
//+++-
"factor((+a*x+b)*(+c*x-d),x)",
"(b+a*x)*(-d+c*x)",
"(+a*x+b)*(+c*x-d)-(b+a*x)*(-d+c*x)",
"0",
//++-+
"factor((+a*x+b)*(-c*x+d),x)",
"-(b+a*x)*(-d+c*x)",
"(+a*x+b)*(-c*x+d)+(b+a*x)*(-d+c*x)",
"0",
//++--
"factor((+a*x+b)*(-c*x-d),x)",
"-(b+a*x)*(d+c*x)",
"(+a*x+b)*(-c*x-d)+(b+a*x)*(d+c*x)",
"0",
//+-++
"factor((+a*x-b)*(+c*x+d),x)",
"(d+c*x)*(-b+a*x)",
"(+a*x-b)*(+c*x+d)-(d+c*x)*(-b+a*x)",
"0",
//+-+-
"factor((+a*x-b)*(+c*x-d),x)",
"(-b+a*x)*(-d+c*x)",
"(+a*x-b)*(+c*x-d)-(-b+a*x)*(-d+c*x)",
"0",
//+--+
"factor((+a*x-b)*(-c*x+d),x)",
"-(-b+a*x)*(-d+c*x)",
"(+a*x-b)*(-c*x+d)+(-b+a*x)*(-d+c*x)",
"0",
//+---
"factor((+a*x-b)*(-c*x-d),x)",
"-(d+c*x)*(-b+a*x)",
"(+a*x-b)*(-c*x-d)+(d+c*x)*(-b+a*x)",
"0",
//-+++
"factor((-a*x+b)*(+c*x+d),x)",
"-(d+c*x)*(-b+a*x)",
"(-a*x+b)*(+c*x+d)+(d+c*x)*(-b+a*x)",
"0",
//-++-
"factor((-a*x+b)*(+c*x-d),x)",
"-(-b+a*x)*(-d+c*x)",
"(-a*x+b)*(+c*x-d)+(-b+a*x)*(-d+c*x)",
"0",
//-+-+
"factor((-a*x+b)*(-c*x+d),x)",
"(-b+a*x)*(-d+c*x)",
"(-a*x+b)*(-c*x+d)-(-b+a*x)*(-d+c*x)",
"0",
//-+--
"factor((-a*x+b)*(-c*x-d),x)",
"(d+c*x)*(-b+a*x)",
"(-a*x+b)*(-c*x-d)-(d+c*x)*(-b+a*x)",
"0",
//--++
"factor((-a*x-b)*(+c*x+d),x)",
"-(b+a*x)*(d+c*x)",
"(-a*x-b)*(+c*x+d)+(b+a*x)*(d+c*x)",
"0",
//--+-
"factor((-a*x-b)*(+c*x-d),x)",
"-(b+a*x)*(-d+c*x)",
"(-a*x-b)*(+c*x-d)+(b+a*x)*(-d+c*x)",
"0",
//---+
"factor((-a*x-b)*(-c*x+d),x)",
"(b+a*x)*(-d+c*x)",
"(-a*x-b)*(-c*x+d)-(b+a*x)*(-d+c*x)",
"0",
//----
"factor((-a*x-b)*(-c*x-d),x)",
"(b+a*x)*(d+c*x)",
"(-a*x-b)*(-c*x-d)-(b+a*x)*(d+c*x)",
"0",
// this used to cause divide by zero
// fixed by calling ispoly before calling coeff
// "factor(1/x+1)",
// "(1+x)/x",
// see if poly gets rationalized
// "(x+1)(x+2)(x+3)/x^3",
// "1+6/(x^3)+11/(x^2)+6/x",
// "factor(last)",
// "(1+x)*(2+x)*(3+x)/(x^3)",
// this used to fail
"factor(x,x)",
"x",
"factor(x^2,x)",
"x^2",
"factor(x^3,x)",
"x^3",
"bake=1",
"",
"y=(x+1)(x+2)",
"",
"factor(y,z)",
"x^2+3*x+2",
"factor(y,y)",
"x^2+3*x+2",
"y=x^2+exp(x)",
"",
"factor(y)",
"x^2+exp(x)",
};
void
test_factorpoly(void)
{
test(__FILE__, s, sizeof s / sizeof (char *));
}
#endif
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/zhouxs1023/eigenmath_pratt.git
git@gitee.com:zhouxs1023/eigenmath_pratt.git
zhouxs1023
eigenmath_pratt
eigenmath_pratt
master

搜索帮助