Polynomial long division: Difference between revisions

Content added Content deleted
Line 290: Line 290:
poly.popBack();
poly.popBack();
return (cast(int)poly.length) - 1;
return (cast(int)poly.length) - 1;
}

static T[] mul(T)(T item, int n) {
return array(take(repeat(item), n));
}
}


Line 302: Line 298:
throw new Exception("ZeroDivisionError");
throw new Exception("ZeroDivisionError");
if (dN >= dD) {
if (dN >= dD) {
q = mul(0.0, dN);
q = array(take(repeat(0.0), dN));
while (dN >= dD) {
while (dN >= dD) {
double[] d = mul(0.0, dN - dD) ~ D;
auto d = array(take(repeat(0.0), dN - dD)) ~ D;
double mult = q[dN - dD] = N[$-1] / d[$-1];
double mult = q[dN - dD] = N[$-1] / d[$-1];
d[] *= mult;
d[] *= mult;
Line 312: Line 308:
r = N;
r = N;
} else {
} else {
q = [0];
q = [0.0];
r = N;
r = N;
}
}
Line 322: Line 318:
double[] D = [-3.0, 1.0, 0.0, 0.0];
double[] D = [-3.0, 1.0, 0.0, 0.0];
auto qr = polyDiv(N, D);
auto qr = polyDiv(N, D);
writeln("Polynomial Long Division:");
writefln(" %s / %s = %s remainder %s", N, D, qr._0, qr._1);
writefln(" %s / %s = %s remainder %s", N, D, qr._0, qr._1);
}</lang>
}</lang>