Formal power series: Difference between revisions

Content added Content deleted
(Added Wren)
(→‎{{header|Wren}}: Corrected previously unused inverseCoef method and added more examples.)
Line 3,175: Line 3,175:
var res = List.filled(n+1, null)
var res = List.filled(n+1, null)
res[0] = _term[0].inverse
res[0] = _term[0].inverse
for (i in 1..n) {
if (n > 0) {
for (j in 0...i) res[i] = res[i] + _term[i - j] * res[j]
for (i in 1..n) {
res[i] = -res[0] * res[i]
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]
return res[n]
Line 3,224: Line 3,227:
_fps = fps
_fps = fps
_other = other
_other = other
}
}
coef(n) {
coef(n) {
var res = Rat.zero
var res = Rat.zero
Line 3,254: Line 3,257:
var c = _term[0]
var c = _term[0]
Rat.showAsInt = true
Rat.showAsInt = true
var supers = ["⁰", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹", "¹⁰", "¹¹"]
if (c != Rat.zero) sb = sb + c.toString
if (c != Rat.zero) sb = sb + c.toString
for (i in 1...dpTerm) {
for (i in 1...dpTerm) {
Line 3,264: Line 3,268:
(c == Rat.minusOne) ? " - %(xvar)" :
(c == Rat.minusOne) ? " - %(xvar)" :
(c.num < 0) ? " - %(-c)%(xvar)" : "%(c)%(xvar)")
(c.num < 0) ? " - %(-c)%(xvar)" : "%(c)%(xvar)")
if (i > 1) sb = sb + "^%(i)"
if (i > 1) sb = sb + "%(supers[i])"
}
}
}
}
Line 3,277: Line 3,281:
var cos = FormalPS.new()
var cos = FormalPS.new()
var sin = cos.intg()
var sin = cos.intg()
var tan = sin/cos
cos.copyFrom(FormalPS.fromPolynomial([Rat.one]) - sin.intg())
cos.copyFrom(FormalPS.fromPolynomial([Rat.one]) - sin.intg())
System.print("SIN(x) = %(sin)")
System.print("sin(x) = %(sin)")
System.print("COS(x) = %(cos)")</lang>
System.print("cos(x) = %(cos)")
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}}
{{out}}
<pre>
<pre>
SIN(x) = x - 1/6x^3 + 1/120x^5 - 1/5040x^7 + 1/362880x^9 - 1/39916800x^11 + ...
sin(x) = x - 1/6x³ + 1/120x⁵ - 1/5040x⁷ + 1/362880x⁹ - 1/39916800x¹¹ + ...
COS(x) = 1 - 1/2x^2 + 1/24x^4 - 1/720x^6 + 1/40320x^8 - 1/3628800x^10 + ...
cos(x) = 1 - 1/2x² + 1/24x⁴ - 1/720x⁶ + 1/40320x⁸ - 1/3628800x¹⁰ + ...
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>
</pre>