Skip to content

Commit

Permalink
* lib/calculus/integration.gel: Add TrapezoidRule, LeftHandRule,
Browse files Browse the repository at this point in the history
	  RightHandRule

	* help/C/genius.xml: Document the new functions

	* src/geniustests.txt: add some tests
  • Loading branch information
jirilebl committed Oct 4, 2023
1 parent e017ded commit 85f7286
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 5 deletions.
9 changes: 9 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
Tue Oct 03 23:23:52 2023 Jiri (George) Lebl <jirka@5z.com>

* lib/calculus/integration.gel: Add TrapezoidRule, LeftHandRule,
RightHandRule

* help/C/genius.xml: Document the new functions

* src/geniustests.txt: add some tests

Tue Oct 03 22:58:01 2023 Jiri (George) Lebl <jirka@5z.com>

* src/graphing.c: The shortcut of inputting just the function name
Expand Down
29 changes: 27 additions & 2 deletions help/C/genius.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7353,6 +7353,14 @@ or <varname>b</varname> can be <constant>null</constant>.</para>
</listitem>
</varlistentry>

<varlistentry>
<term><anchor id="gel-function-LeftHandRule"/>LeftHandRule</term>
<listitem>
<synopsis>LeftHandRule (f,a,b,n)</synopsis>
<para>Integration by left hand rule on the interval [a,b] with n subintervals.</para>
</listitem>
</varlistentry>

<varlistentry>
<term><anchor id="gel-function-LeftLimit"/>LeftLimit</term>
<listitem>
Expand All @@ -7373,7 +7381,7 @@ or <varname>b</varname> can be <constant>null</constant>.</para>
<term><anchor id="gel-function-MidpointRule"/>MidpointRule</term>
<listitem>
<synopsis>MidpointRule (f,a,b,n)</synopsis>
<para>Integration by midpoint rule.</para>
<para>Integration by midpoint rule on the interval [a,b] with n subintervals.</para>
</listitem>
</varlistentry>

Expand Down Expand Up @@ -7525,7 +7533,8 @@ computed by numerical integration using
<term><anchor id="gel-function-NumericalIntegral"/>NumericalIntegral</term>
<listitem>
<synopsis>NumericalIntegral (f,a,b)</synopsis>
<para>Integration by rule set in NumericalIntegralFunction of f from a to b using NumericalIntegralSteps steps.</para>
<para>Integration by rule set in NumericalIntegralFunction of f from a to b using NumericalIntegralSteps steps.
By default NumericalIntegralFunction is the CompositeSimpsonsRule.</para>
</listitem>
</varlistentry>

Expand Down Expand Up @@ -7605,6 +7614,14 @@ and has period <userinput>b-a</userinput>.</para>
</listitem>
</varlistentry>

<varlistentry>
<term><anchor id="gel-function-RightHandRule"/>RightHandRule</term>
<listitem>
<synopsis>RightHandRule (f,a,b,n)</synopsis>
<para>Integration by right hand rule on the interval [a,b] with n subintervals.</para>
</listitem>
</varlistentry>

<varlistentry>
<term><anchor id="gel-function-RightLimit"/>RightLimit</term>
<listitem>
Expand All @@ -7613,6 +7630,14 @@ and has period <userinput>b-a</userinput>.</para>
</listitem>
</varlistentry>

<varlistentry>
<term><anchor id="gel-function-TrapezoidRule"/>TrapezoidRule</term>
<listitem>
<synopsis>TrapezoidRule (f,a,b,n)</synopsis>
<para>Integration by trapezoid rule on the interval [a,b] with n subintervals.</para>
</listitem>
</varlistentry>

<varlistentry>
<term><anchor id="gel-function-TwoSidedFivePointFormula"/>TwoSidedFivePointFormula</term>
<listitem>
Expand Down
61 changes: 59 additions & 2 deletions lib/calculus/integration.gel
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ function CompositeSimpsonsRuleTolerance(f,a,b,FourthDerivativeBound,Tolerance) =
CompositeSimpsonsRule (f, a, b, n)
)

# FIXME: reference, though this is really stupid
SetHelp ("MidpointRule", "calculus", "Integration by midpoint rule")
function MidpointRule(f,a,b,n) =
(
Expand All @@ -58,10 +57,68 @@ function MidpointRule(f,a,b,n) =
(s*len)/n
)

SetHelp ("TrapezoidRule", "calculus", "Integration by trapezoid rule")
function TrapezoidRule(f,a,b,n) =
(
local *;
if(not IsFunction(f)) then
(error("TrapezoidRule: argument 1 must be a function");bailout)
else if(not IsReal(a) or not IsReal(b)) then
(error("TrapezoidRule: arguments 2, 3 must be real values");bailout)
else if(not IsInteger(n)) then
(error("TrapezoidRule: argument 4 must be an integer");bailout);
## check bounds
if(a>b) then (error("TrapezoidRule: argument 2 must be less than or equal to argument 3");bailout)
else if(n<= 0) then (error("TrapezoidRule: argument 4 must be positive");bailout);

len = float(b-a);
s = sum i=1 to (n-1) do float(2*f(a+(len*i)/n));
s = s+f(a)+f(b);
(s*len)/(2*n)
)

SetHelp ("LeftHandRule", "calculus", "Integration by left hand rule")
function LeftHandRule(f,a,b,n) =
(
local *;
if(not IsFunction(f)) then
(error("LeftHandRule: argument 1 must be a function");bailout)
else if(not IsReal(a) or not IsReal(b)) then
(error("LeftHandRule: arguments 2, 3 must be real values");bailout)
else if(not IsInteger(n)) then
(error("LeftHandRule: argument 4 must be an integer");bailout);
## check bounds
if(a>b) then (error("LeftHandRule: argument 2 must be less than or equal to argument 3");bailout)
else if(n<= 0) then (error("LeftHandRule: argument 4 must be positive");bailout);

len = float(b-a);
s = sum i=0 to (n-1) do float(f(a+(len*i)/n));
(s*len)/n
)

SetHelp ("RightHandRule", "calculus", "Integration by right hand rule")
function RightHandRule(f,a,b,n) =
(
local *;
if(not IsFunction(f)) then
(error("RightHandRule: argument 1 must be a function");bailout)
else if(not IsReal(a) or not IsReal(b)) then
(error("RightHandRule: arguments 2, 3 must be real values");bailout)
else if(not IsInteger(n)) then
(error("RightHandRule: argument 4 must be an integer");bailout);
## check bounds
if(a>b) then (error("RightHandRule: argument 2 must be less than or equal to argument 3");bailout)
else if(n<= 0) then (error("RightHandRule: argument 4 must be positive");bailout);

len = float(b-a);
s = sum i=1 to n do float(f(a+(len*i)/n));
(s*len)/n
)

SetHelp ("NumericalIntegralSteps", "parameters", "Steps to perform in NumericalIntegral")
parameter NumericalIntegralSteps = 1000

SetHelp ("NumericalIntegralFunction", "parameters", "The function used for numerical integration in NumericalIntegral")
SetHelp ("NumericalIntegralFunction", "parameters", "The function used for numerical integration in NumericalIntegral (by default CompositeSimpsonsRule)")
parameter NumericalIntegralFunction = `CompositeSimpsonsRule

SetHelp ("NumericalIntegral", "calculus", "Integration by rule set in NumericalIntegralFunction of f from a to b using NumericalIntegralSteps steps")
Expand Down
5 changes: 4 additions & 1 deletion lib/library-strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ char *fake = N_("How many successive steps to be within tolerance for calculatio
char *fake = N_("Tolerance for calculating the derivatives of functions");
char *fake = N_("Tolerance of the ErrorFunction (used for complex values only)");
char *fake = N_("Tolerance of the GaussDistribution function");
char *fake = N_("The function used for numerical integration in NumericalIntegral");
char *fake = N_("The function used for numerical integration in NumericalIntegral (by default CompositeSimpsonsRule)");
char *fake = N_("Steps to perform in NumericalIntegral");
char *fake = N_("How many iterations to try for InfiniteSum and InfiniteProduct");
char *fake = N_("How many successive steps to be within tolerance for InfiniteSum and InfiniteProduct");
Expand Down Expand Up @@ -191,6 +191,7 @@ char *fake = N_("Try to calculate an infinite sum for a single parameter functio
char *fake = N_("Try to calculate an infinite sum for a double parameter function with func(arg,n)");
char *fake = N_("Try and see if a real-valued function is continuous at x0 by calculating the limit there");
char *fake = N_("Test for differentiability by approximating the left and right limits and comparing");
char *fake = N_("Integration by left hand rule");
char *fake = N_("Calculate the left limit of a real-valued function at x0");
char *fake = N_("Calculate the limit of a real-valued function at x0. Tries to calculate both left and right limits.");
char *fake = N_("Integration by midpoint rule");
Expand All @@ -209,7 +210,9 @@ char *fake = N_("Return a function which is the odd periodic extension of f defi
char *fake = N_("Compute one-sided derivative using five point formula");
char *fake = N_("Compute one-sided derivative using three-point formula");
char *fake = N_("Return a function which is the periodic extension of f defined on the interval [a,b]");
char *fake = N_("Integration by right hand rule");
char *fake = N_("Calculate the right limit of a real-valued function at x0");
char *fake = N_("Integration by trapezoid rule");
char *fake = N_("Compute two-sided derivative using five-point formula");
char *fake = N_("Compute two-sided derivative using three-point formula");
char *fake = N_("argument (angle) of complex number");
Expand Down
4 changes: 4 additions & 0 deletions src/geniustests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,10 @@ EulersMethod (`(x,y)=[(x-y@(1));(x-y@(2))],0,[0;0],3,3) [2.0;2.0]
CompositeSimpsonsRule(`(x)=x^2,0,3,100) 9.0
CompositeSimpsonsRule(`(x)=x^2,3,0,100) -9.0
CompositeSimpsonsRule(`(x)=x^2,3,3,100) 0
LeftHandRule(`(x)=x^2,0,2,4) 1.75
RightHandRule(`(x)=x^2,0,2,4) 3.75
MidpointRule(`(x)=x^2,0,2,4) 2.625
TrapezoidRule(`(x)=x^2,0,2,4) 2.75
a a
a=7;UndefineAll();a a
UndefineAll();a a
Expand Down

0 comments on commit 85f7286

Please sign in to comment.