Re: Axiom programs which include functions?
Ralf Hemmecke <[email protected]>
| Newsgroups | gmane.comp.mathematics.axiom.user |
|---|---|
| Message-ID | <[email protected]> |
Hi Alasdair,
On 05/19/2008 04:39 PM, Alasdair McAndrew wrote:
> Hi,
>
> I can write (simple) Axiom programs for which the parameters are all
> numbers, but what if the parameters include a function? Suppose I
> wished to write a program to, say, solve the equation f(x)=0 by the
> bisection method, and I wanted to call it as
>
> bisect(f,a,b)
>
> where [a,b] brackets a solution. How do I do this? And in what form
> does Axiom like its functions in such a program?
A function in Axiom is not so much different from a number. Just do the
obvious.
Suppose you write a function of type
bisect(f: Float -> Float, a: Float, b: Float): Record(result: Float,
eps: Float) ==
...
where the record stores an approximate result together with an error.
---BEGIN aaa.spad
)abbrev domain AAA Aaa
Aaa: with
bisect: (Float -> Float, Float, Float) -> Record(x: Float, eps: Float)
== add
bisect(f: Float -> Float, a: Float, b: Float): Record(x: Float,
eps: Float) ==
-- insert your bisection algorithm here
r: Record(x: Float, eps: Float) := [f a, b-a]
--------------------------------------------^
-- This is an example of the application of the input parameter f.
---END aaa.spad
Then go to axiom and say:
)co aaa.spad
bisect(sin, 1.0, 2.0)
bisect(cos, 1.0, 2.0)
Does that help?
Ralf