/usr/share/yacas/simplify.rep/factorial.ys is in yacas 1.3.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | /* FactorialSimplify algorithm:
1) expand binomials into factors
2) expand brackets as much as possible
3) for the remaining rational expressions x/y,
take all the factors of x and y, and match them
up one by one to determine if they can be
factored out. The algorithm will look at expressions like x^n/x^m
where (n-m) is an integer, or at expressions x!/y! where (x-y)
is an integer. The routine CommonDivisors does these steps, and
returns the new numerator and denominator factor.
FactorialSimplifyWorker does the actual O(n^2) algorithm of
matching all terms up.
*/
FactorialNormalForm(x):=
[
// Substitute binomials
x:=(x/:{Bin(_n,_m)<- (n!)/((m!)*(n-m)!)});
// Expand expression as much as possible so that the terms become
// simple rationals.
x:=(
x/::Hold({
(_a/_b)/_c <- (a)/(b*c),
(-(_a/_b))/_c <- (-a)/(b*c),
(_a/_b)*_c <- (a*c)/b,
(_a*_b)^_m <- a^m*b^m,
(_a/_b)^_m*_c <- (a^m*c)/b^m,
_a*(_b+_c) <- a*b+a*c,
(_b+_c)*_a <- a*b+a*c,
(_b+_c)/_a <- b/a+c/a,
_a*(_b-_c) <- a*b-a*c,
(_b-_c)*_a <- a*b-a*c,
(_b-_c)/_a <- b/a-c/a
}));
x;
];
FactorialSimplify(x):=
[
x := FactorialNormalForm(x);
FactorialSimplifyWorker(x);
];
/* CommonDivisors takes two parameters x and y as input, determines a common divisor g
and then returns {x/g,y/g,g}.
*/
10 # CommonDivisors(_x^(_n),_x^(_m)) <-- {x^Simplify(n-m),1,x^m};
10 # CommonDivisors(_x^(_n),_x) <-- {x^Simplify(n-1),1,x};
10 # CommonDivisors(_x,_x^(_m)) <-- {x^Simplify(1-m),1,x^m};
10 # CommonDivisors((_x) !,_x) <-- {(x-1)!,1,x};
10 # CommonDivisors(_x,_x) <-- {1,1,x};
10 # CommonDivisors(- _x,_x) <-- {-1,1,x};
10 # CommonDivisors(_x,- _x) <-- {1,-1,x};
10 # CommonDivisors((_x),(_x)!) <-- {1,(x-1)!,x};
10 # CommonDivisors((_x)!, (_y)!)_IsInteger(Simplify(x-y)) <-- CommonFact(Simplify(x-y),y);
10 # CommonDivisors((_x)! ^ _m, (_y)! ^ _m)_IsInteger(Simplify(x-y)) <-- CommonFact(Simplify(x-y),y)^m;
10 # CommonFact(dist_IsNegativeInteger,_y)
<-- {1,Factorize(i,1,-dist,Simplify(y+i+dist)),Simplify(y+dist)!};
11 # CommonFact(_dist,_y)
<-- {Factorize(i,1,dist,Simplify(y+i)),1,Simplify(y)!};
60000 # CommonDivisors(_x,_y) <-- {x,y,1};
10 # CommonFactors((_x)!,_y)_(Simplify(y-x) = 1) <-- {y!,1};
10 # CommonFactors((_x)!,_y)_(Simplify((-y)-x) = 1) <-- {(-y)!,-1};
10 # CommonFactors(_x^_n,_x^_m) <-- {x^Simplify(n+m),1};
10 # CommonFactors(_x^_n,_x) <-- {x^Simplify(n+1),1};
60000 # CommonFactors(_x,_y) <-- {x,y};
10 # FactorialSimplifyWorker(_x+_y) <-- FactorialSimplifyWorker(x)+FactorialSimplifyWorker(y);
10 # FactorialSimplifyWorker(_x-_y) <-- FactorialSimplifyWorker(x)-FactorialSimplifyWorker(y);
10 # FactorialSimplifyWorker( -_y) <-- -FactorialSimplifyWorker(y);
LocalSymbols(x,y,i,j,n,d)[
20 # FactorialSimplifyWorker(_x/_y) <--
[
// first separate out factors of the denominator
Local(numerCommon,numerTerms);
{numerCommon,numerTerms}:=FactorialGroupCommonDivisors(x);
Local(denomCommon,denomTerms);
{denomCommon,denomTerms}:=FactorialGroupCommonDivisors(y);
Local(n,d,c);
{n,d,c} := FactorialDivideTerms(numerCommon,denomCommon);
(n/d)*Simplify((numerTerms)/(denomTerms));
];
20 # FactorialGcd(_x,_y) <--
[
// first separate out factors of the denominator
Local(numerCommon,numerTerms);
{numerCommon,numerTerms}:=FactorialGroupCommonDivisors(x);
Local(denomCommon,denomTerms);
{denomCommon,denomTerms}:=FactorialGroupCommonDivisors(y);
Local(n,d,c);
{n,d,c} := FactorialDivideTerms(numerCommon,denomCommon);
c;
];
10 # FactorialDivideTerms(- _x,- _y) <-- FactorialDivideTermsAux(x,y);
LocalSymbols(n,d,c)
[
20 # FactorialDivideTerms(- _x, _y)
<--
[
Local(n,d,c);
{n,d,c} := FactorialDivideTermsAux(x,y);
{-n,d,c};
];
30 # FactorialDivideTerms( _x,- _y)
<--
[
Local(n,d,c);
{n,d,c} := FactorialDivideTermsAux(x,y);
{n,-d,c};
];
];
40 # FactorialDivideTerms( _x, _y)
<--
[
// Echo("GOTHERE 40");
FactorialDivideTermsAux(x,y);
];
LocalSymbols(n,d,c)
[
10 # FactorialDivideTermsAux(_x,_y) <--
[
x:=Flatten(x,"*");
y:=Flatten(y,"*");
Local(i,j,common);
common:=1;
For(i:=1,i<=Length(x),i++)
For(j:=1,j<=Length(y),j++)
[
Local(n,d,c);
//Echo("inp is ",x[i]," ",y[j]);
{n,d,c} := CommonDivisors(x[i],y[j]);
//Echo("aux is ",{n,d,c});
x[i] := n;
y[j] := d;
common:=common*c;
];
//Echo("final ",{x,y,common});
//Echo("finalor ",{Factorize(x),Factorize(y),common});
{Factorize(x),Factorize(y),common};
];
];
];
60000 # FactorialSimplifyWorker(_x)
<--
[
// first separate out factors of the denominator
Local(numerCommon,numerTerms);
{numerCommon,numerTerms}:=FactorialGroupCommonDivisors(x);
numerCommon*numerTerms;
];
/* FactorialFlattenAddition accepts an expression of form a+b+c-d+e-f+ ... +z with arbitrary additions
and subtractions, and converts it to a list of terms. Terms that need to be subtracted start with a
negation sign (useful for pattern matching).
*/
10 # FactorialFlattenAddition(_x+_y) <-- Concat(FactorialFlattenAddition(x), FactorialFlattenAddition(y));
10 # FactorialFlattenAddition(_x-_y) <-- Concat(FactorialFlattenAddition(x),-FactorialFlattenAddition(y));
10 # FactorialFlattenAddition( -_y) <-- -FactorialFlattenAddition(y);
20 # FactorialFlattenAddition(_x ) <-- {x};
LocalSymbols(n,d,c)
[
10 # FactorialGroupCommonDivisors(_x) <--
[
Local(terms,common,tail);
terms:=FactorialFlattenAddition(x);
//Echo("terms is ",terms);
common := Head(terms);
tail:=Tail(terms);
While (tail != {})
[
Local(n,d,c);
{n,d,c} := FactorialDivideTerms(common,Head(tail));
//Echo(common, " ",Head(tail)," ",c);
common := c;
tail:=Tail(tail);
];
Local(i,j);
// Echo("common is ",common);
For(j:=1,j<=Length(terms),j++)
[
Local(n,d,c);
// Echo("IN = ",terms[j]," ",common);
// Echo("n = ",n);
{n,d,c} := FactorialDivideTerms(terms[j],common);
// Echo("n = ",n);
// Echo("{n,d,c} = ",{n,d,c});
Check(d = 1,
ToString()[
Echo("FactorialGroupCommonDivisors failure 1 : ",d);
]);
/*
Check(Simplify(c-common) = 0,
ToString()
[
Echo("FactorialGroupCommonDivisors failure 2 : ");
Echo(c," ",common);
Echo(Simplify(c-common));
]);
*/
terms[j] := n;
];
terms:=Add(terms);
common:=Flatten(common,"*");
For(j:=1,j<=Length(common),j++)
[
Local(f1,f2);
{f1,f2}:=CommonFactors(common[j],terms);
common[j]:=f1;
terms:=f2;
For(i:=1,i<=Length(common),i++)
If(i != j,
[
{f1,f2}:=CommonFactors(common[j],common[i]);
common[j]:=f1;
common[i]:=f2;
]);
];
common := Factorize(common);
{common,terms};
];
];
|