Formal power series: Difference between revisions

→‎{{header|Wren}}: Corrected previously unused inverseCoef method and added more examples.
(Added Wren)
(→‎{{header|Wren}}: Corrected previously unused inverseCoef method and added more examples.)
Line 3,175:
var res = List.filled(n+1, null)
res[0] = _term[0].inverse
forif (in in> 1..n0) {
for (ji in 01...in) res[i] = res[i] + _term[i - j] * res[j]{
res[i] = -res[0] * res[i] = Rat.zero
for (j in 0...i) res[i] = res[i] + _term[i - j] * res[j]
res[i] = -res[0] * res[i]
}
}
return res[n]
Line 3,224 ⟶ 3,227:
_fps = fps
_other = other
}
coef(n) {
var res = Rat.zero
Line 3,254 ⟶ 3,257:
var c = _term[0]
Rat.showAsInt = true
var supers = ["⁰", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹", "¹⁰", "¹¹"]
if (c != Rat.zero) sb = sb + c.toString
for (i in 1...dpTerm) {
Line 3,264 ⟶ 3,268:
(c == Rat.minusOne) ? " - %(xvar)" :
(c.num < 0) ? " - %(-c)%(xvar)" : "%(c)%(xvar)")
if (i > 1) sb = sb + "^%(supers[i])"
}
}
Line 3,277 ⟶ 3,281:
var cos = FormalPS.new()
var sin = cos.intg()
var tan = sin/cos
cos.copyFrom(FormalPS.fromPolynomial([Rat.one]) - sin.intg())
System.print("SINsin(x) = %(sin)")
System.print("COScos(x) = %(cos)")</lang>
System.print("tan(x) = %(tan)")
System.print("sin'(x) = %(sin.diff())")
var exp = FormalPS.new()
exp.copyFrom(FormalPS.fromPolynomial([Rat.one]) + exp.intg())
System.print("exp(x) = %(exp)")</lang>
 
{{out}}
<pre>
SINsin(x) = x - 1/6x^3³ + 1/120x^5120x⁵ - 1/5040x^75040x⁷ + 1/362880x^9362880x⁹ - 1/39916800x^11¹¹ + ...
COScos(x) = 1 - 1/2x^2² + 1/24x^424x⁴ - 1/720x^6720x⁶ + 1/40320x^840320x⁸ - 1/3628800x^10¹⁰ + ...
tan(x) = x + 1/3x³ + 2/15x⁵ + 17/315x⁷ + 62/2835x⁹ + 1382/155925x¹¹ + ...
sin'(x) = 1 - 1/2x² + 1/24x⁴ - 1/720x⁶ + 1/40320x⁸ - 1/3628800x¹⁰ + ...
exp(x) = 1 + x + 1/2x² + 1/6x³ + 1/24x⁴ + 1/120x⁵ + 1/720x⁶ + 1/5040x⁷ + 1/40320x⁸ + 1/362880x⁹ + 1/3628800x¹⁰ + 1/39916800x¹¹ + ...
</pre>
 
9,476

edits