Polynomial derivative: Difference between revisions

add FreeBASIC
(julia example)
(add FreeBASIC)
Line 40:
Derivative of -x4-x3+x+1: 1 - 3*x^2 - 4*x^3
</pre>
 
 
=={{header|FreeBASIC}}==
<lang freebasic>sub polydiff( p() as integer )
'differentiates the polynomial
'p(0) + p(1)x + p(2)x^2 +... + p(n)x^n
'in place
dim as integer i, n = ubound(p)
if n=0 then
p(0)=0
return
end if
for i = 0 to n - 1
p(i) = (i+1)*p(i+1)
next i
redim preserve p(0 to n-1)
return
end sub
 
sub print_poly( p() as integer )
'quick and dirty display of the poly
if ubound(p)=0 and p(0)=0 then
print 0
return
end if
for i as integer = 0 to ubound(p)
if i = 0 then print p(i);" ";
if i = 1 and p(i)>0 then print using "+ #x";p(i);
if i = 1 and p(i)<0 then print using "- #x";-p(i);
if i > 1 and p(i)>0 then print using "+ #x^#";p(i);i;
if i > 1 and p(i)<0 then print using "- #x^#";-p(i);i;
next i
print
end sub
 
'test cases
redim as integer p(0)
p(0) = 5
print_poly(p())
print "Differentiates to "
polydiff(p())
print_poly(p()): print
 
redim as integer p(1)
p(0) = 4 : p(1) = -3
print_poly(p())
print "Differentiates to "
polydiff(p())
print_poly(p()): print
 
redim as integer p(2)
p(0) = -1 : p(1) = 6 : p(2) = 5
print_poly(p())
print "Differentiates to "
polydiff(p())
print_poly(p()): print
 
redim as integer p(3)
p(0) = 4 : p(1) = 3 : p(2) = -2 : p(3) = 1
print_poly(p())
print "Differentiates to "
polydiff(p())
print_poly(p()): print
 
redim as integer p(4)
p(0) = 1 : p(1) = 1 : p(2) = 0 : p(3) = -1 : p(4) = -1
print_poly(p())
print "Differentiates to "
polydiff(p())
print_poly(p()): print</lang>
{{out}}<pre>
5
Differentiates to
0
 
4 - 3x
Differentiates to
-3
 
-1 + 6x+ 5x^2
Differentiates to
6 + %10x
 
4 + 3x- 2x^2+ 1x^3
Differentiates to
3 - 4x+ 3x^2
 
1 + 1x- 1x^3- 1x^4
Differentiates to
1 - 3x^2- 4x^3</pre>
781

edits