Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Reduce (computer algebra system)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Algebraic programming examples == The [[:File:REDUCE Screenshot 1.png|screenshot]] shows simple interactive use. As a simple programming example, consider the problem of computing the <math>n</math><sup>th</sup> [[Taylor series|Taylor polynomial]] of the function <math>f(x)</math> about the point <math>x=a</math>, which is given by the formula <math>\sum_{r=0}^n \frac{f^{(r)}(a)}{r!} (x-a)^{r}</math>. Here, <math>f^{(r)}</math> denotes the <math>r</math><sup>th</sup> [[derivative]] of <math>f</math> evaluated at the point <math>a</math> and <math>r!</math> denotes the [[factorial]] of <math>r</math>. (However, note that REDUCE includes [https://reduce-algebra.sourceforge.io/manual-lookup.php?Series%20Expansion sophisticated facilities] for [[Power series|power-series]] expansion.) As an ''example of functional programming'' in REDUCE, here is an easy way to compute the 5th Taylor polynomial of <math>\sin x</math> about 0. In the following code, the control variable <code>r</code> takes values from 0 through 5 in steps of 1, <code>df</code> is the REDUCE [[Derivative|differentiation]] operator and the operator <code>sub</code> performs substitution of its first argument into its second. Note that this code is very similar to the mathematical formula above (with <math>n=5</math> and <math>a=0</math>). <!-- REDUCE syntax highlighting is not (yet) supported, but Octave has some similarities and supports %-comments. --> <syntaxhighlight lang="octave" copy> for r := 0:5 sum sub(x = 0, df(sin x, x, r))*x^r/factorial r; </syntaxhighlight> produces by default the output<ref>The typeset REDUCE output shown assumes the use of [https://reduce-algebra.sourceforge.io/versions.php#csl CSL REDUCE].</ref> <div class="center" style="width: auto; margin-left: auto; margin-right: auto;"><math> \frac{x\left(x^4-20x^2+120\right)}{120} </math></div> This is correct, but it doesn't look much like a Taylor series. That can be fixed by changing a few output-control switches and then evaluating the special variable <code>ws</code>, which stands for <u>w</u>ork<u>s</u>pace and holds the last non-empty output expression: <syntaxhighlight lang="octave" copy> off allfac; on revpri, div; ws; </syntaxhighlight> <div class="center" style="width: auto; margin-left: auto; margin-right: auto;"><math> x-\frac{1}{6}x^3+\frac{1}{120}x^5 </math></div> As an ''example of procedural programming'' in REDUCE, here is a procedure to compute the general Taylor polynomial, which works for functions that are well-behaved at the expansion point <math>a</math>. {{anchor|Procedural programming}} <syntaxhighlight lang="octave" copy> procedure my_taylor(f, x, x0, n); % Return the nth Taylor polynomial of f % as a function of x about x0. begin scalar result := sub(x = x0, f), mul := 1; for r := 1:n do << f := df(f, x); mul := mul*(x - x0)/r; result := result + sub(x = x0, f)*mul >>; return result end; </syntaxhighlight> The procedure is called <code>my_taylor</code> because REDUCE already includes an operator called <code>taylor</code>. All the text following a <code>%</code> sign up to the end of the line is a comment. The keyword <code>scalar</code> introduces and initializes two [[Local variable|local variables]], <code>result</code> and <code>mul</code>. The keywords <code>begin</code> and <code>end</code> delimit a block of code that may include local variables and may return a value, whereas the symbols <code><<</code> and <code>>></code> delimit a group of statements without introducing local variables. The procedure may be called as follows to compute the same Taylor polynomial as above. <syntaxhighlight lang="octave" copy> my_taylor(sin x, x, 0, 5); </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)