Polynomial derivative: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(6 intermediate revisions by 5 users not shown)
Line 58:
x^3 - 2x^2 + 3x - 4 -> 3x^2 - 4x + 3
-x^4 - x^3 + x + 1 -> -4x^3 - 3x^2 + 1
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
const Poly1: array [0..1-1] of double = (5); {5}
const Poly2: array [0..2-1] of double = (4,-3); {-3x+4}
const Poly3: array [0..3-1] of double = (-1,6,5); {5x^2+6x-1}
const Poly4: array [0..4-1] of double = (-4,3,-2,1); {x^3-2x^2+3x-4}
const Poly5: array [0..5-1] of double = (1,1,0,-1,-1); {-x^4-x^3+x+1}
 
 
 
function GetDerivative(P: array of double): TDoubleDynArray;
var I,N: integer;
begin
SetLength(Result,Length(P)-1);
if Length(P)<1 then exit;
for I:=0 to High(Result) do
Result[I]:= (I+1)*P[I+1];
end;
 
 
function GetPolyStr(D: array of double): string;
{Get polynomial in standard math format string}
var I: integer;
var S: string;
 
function GetSignStr(Lead: boolean; D: double): string;
{Get the sign of coefficient}
begin
Result:='';
if D>0 then
begin
if not Lead then Result:=' + ';
end
else
begin
Result:='-';
if not Lead then Result:=' - ';
end;
end;
 
 
begin
Result:='';
{Get each coefficient}
for I:=High(D) downto 0 do
begin
{Ignore zero values}
if D[I]=0 then continue;
{Get sign and coefficient}
S:=GetSignStr(Result='',D[I]);
S:=S+FloatToStrF(abs(D[I]),ffFixed,18,0);
{Combine with exponents }
if I>1 then Result:=Result+Format('%SX^%d',[S,I])
else if I=1 then Result:=Result+Format('%SX',[S,I])
else Result:=Result+Format('%S',[S]);
end;
end;
 
procedure ShowDerivative(Memo: TMemo; Poly: array of double);
{Show polynomial and and derivative}
var D: TDoubleDynArray;
begin
D:=GetDerivative(Poly);
Memo.Lines.Add('Polynomial: '+GetPolyStr(Poly));
Memo.Lines.Add('Derivative: '+'['+GetPolyStr(D)+']');
Memo.Lines.Add('');
end;
 
 
 
procedure ShowPolyDerivative(Memo: TMemo);
var D: TDoubleDynArray;
begin
ShowDerivative(Memo,Poly1);
ShowDerivative(Memo,Poly2);
ShowDerivative(Memo,Poly3);
ShowDerivative(Memo,Poly4);
ShowDerivative(Memo,Poly5);
end;
</syntaxhighlight>
{{out}}
<pre>
Polynomial: 5
Derivative: []
 
Polynomial: -3X + 4
Derivative: [-3]
 
Polynomial: 5X^2 + 6X - 1
Derivative: [10X + 6]
 
Polynomial: 1X^3 - 2X^2 + 3X - 4
Derivative: [3X^2 - 4X + 3]
 
Polynomial: -1X^4 - 1X^3 + 1X + 1
Derivative: [-4X^3 - 3X^2 + 1]
 
 
Elapsed Time: 17.842 ms.
 
</pre>
 
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
// Polynomial derivative. Nigel Galloway: January 4th., 2023
let n=[[5];[4;-3];[-1;6;5];[-4;3;-2;1];[1;1;0;-1;-1]]|>List.iter((List.mapi(fun n g->n*g)>>List.skip 1>>printfn "%A"))
</syntaxhighlight>
{{out}}
<pre>
[]
[-3]
[6; 10]
[3; -4; 3]
[1; 0; -3; -4]
</pre>
 
Line 355 ⟶ 477:
p = 1 + 1*x - 1*x^3 - 1*x^4
p' = 1 - 3*x^2 - 4*x^</pre>
 
=={{header|J}}==
 
Implementation:
<syntaxhighlight lang=J>pderiv=: -@(1 >. _1+#) {. (* i.@#)</syntaxhighlight>
 
Task examples:
<syntaxhighlight lang=J> pderiv 5
0
pderiv 4 _3
_3
pderiv _1 6 5
6 10
pderiv _4 3 _2 1
3 _4 3
pderiv 1 1 _1 _1
1 _2 _3</syntaxhighlight>
 
Note also that J's <code>p.</code> can be used to apply one of these polynomials to an argument. For example:
 
<syntaxhighlight lang=J> 5 p. 2 3 5 7
5 5 5 5
(pderiv 5) p. 2 3 5 7
0 0 0 0
4 _3 p. 2 3 5 7
_2 _5 _11 _17
(pderiv 4 _3) p. 2 3 5 7
_3 _3 _3 _3</syntaxhighlight>
 
 
=={{header|jq}}==
Line 460 ⟶ 611:
Derivative of x3-2x2+3x-4: 3 - 4*x + 3*x^2
Derivative of -x4-x3+x+1: 1 - 3*x^2 - 4*x^3
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">import std/sequtils
 
type
Polynomial[T] = object
coeffs: seq[T]
Term = tuple[coeff, exp: int]
 
template `[]`[T](poly: Polynomial[T]; idx: Natural): T =
poly.coeffs[idx]
 
template `[]=`[T](poly: var Polynomial; idx: Natural; val: T) =
poly.coeffs[idx] = val
 
template degree(poly: Polynomial): int =
poly.coeffs.high
 
func newPolynomial[T](coeffs: openArray[T]): Polynomial[T] =
## Create a polynomial from a list of coefficients.
result.coeffs = coeffs.toSeq
 
func newPolynomial[T](degree: Natural = 0): Polynomial[T] =
## Create a polynomial with given degree.
## Coefficients are all zeroes.
result.coeffs = newSeq[T](degree + 1)
 
const Superscripts: array['0'..'9', string] = ["⁰", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹"]
 
func superscript(n: Natural): string =
## Return the Unicode string to use to represent an exponent.
if n == 1:
return ""
for d in $n:
result.add Superscripts[d]
 
func `$`(term: Term): string =
## Return the string representation of a term.
if term.coeff == 0: "0"
elif term.exp == 0: $term.coeff
else:
let base = 'x' & superscript(term.exp)
if term.coeff == 1: base
elif term.coeff == -1: '-' & base
else: $term.coeff & base
 
func `$`[T](poly: Polynomial[T]): string =
## Return the string representation of a polynomial.
for idx in countdown(poly.degree, 0):
let coeff = poly[idx]
var term: Term = (coeff: coeff, exp: idx)
if result.len == 0:
result.add $term
elif coeff > 0:
result.add '+'
result.add $term
elif coeff < 0:
term.coeff = -term.coeff
result.add '-'
result.add $term
 
func derivative[T](poly: Polynomial[T]): Polynomial[T] =
## Return the derivative of a polynomial.
if poly.degree == 0: return newPolynomial[T]()
result = newPolynomial[T](poly.degree - 1)
for degree in 1..poly.degree:
result[degree - 1] = degree * poly[degree]
 
for coeffs in @[@[5], @[4, -3], @[-1, 6, 5], @[-4, 3, -2, 1], @[1, 1, 0, -1, -1]]:
let poly = newPolynomial(coeffs)
echo "Polynomial: ", poly
echo "Derivative: ", poly.derivative
echo()
</syntaxhighlight>
 
{{out}}
<pre>Polynomial: 5
Derivative: 0
 
Polynomial: -3x+4
Derivative: -3
 
Polynomial: 5x²+6x-1
Derivative: 10x+6
 
Polynomial: x³-2x²+3x-4
Derivative: 3x²-4x+3
 
Polynomial: -x⁴-x³+x+1
Derivative: -4x³-3x²+1
</pre>
 
Line 615 ⟶ 857:
</pre>
===Classical programming===
Assuming we ignore the existence of the <code>∂</code> operator, here is a typical RPL program that handles polynoms as arrayslists of scalars, asto requiredbe completed by another program to display the tasklist on a fancier way.
≪ → scalarscoeffs
scalarsIF coeffs SIZE 1 - { }
IFTHEN SWAP
THEN { } 1 LAST FOR j
LAST 2 SWAP FORcoeffs j 1 + GET j 1 MAX * +
scalars j GET j 1 - * +
NEXT
ELSE { 0 +}
END
≫ ≫
'DPDX' STO
 
[{5]} DPDX
[{4 -3]} DPDX
[{-1 6 5]} DPDX
[{-4 3 -2 1]} DPDX
[{1 1 0 -1 -1]} DPDX
{{out}}
<pre>
5: { 0 }
4: { -3 }
3: { 6 10 }
2: { 3 -4 3 }
1: { 1 0 -3 -4 }
</pre>
=== Fancy output ===
≪ → coeffs
≪ coeffs 1 GET
coeffs SIZE 2 FOR j
coeffs j GET ‘x’ j 1 - ^ * SWAP +
-1 STEP
≫ COLCT
‘→EQ’ STO
 
{1 1 0 -1 -1} DPDX
DUP →EQ
{{out}}
<pre>
52: [{ 1 0 ]-3 -4 }
41: [ '1-3 ]*x^2-4*x^3'
3: [ 6 10 ]
2: [ 3 -4 3 ]
1: [ 1 0 -3 -4 ]
</pre>
 
Line 687 ⟶ 945:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var derivative = Fn.new { |p|
if (p.count == 1) return [0]
var d = p[1..-1].toList
9,483

edits