User:Coderjoe/Sandbox2: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
No edit summary
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
<lang Ada>
<lang parigp>compose(f,g)={
x -> f(g(x))
with Ada.Text_IO;
};
with Ada.Numerics.Generic_Elementary_Functions;
procedure Program is
generic
type Number_Type is private;
with function F(X : Number_Type) return Number_Type;
with function G(X : Number_Type) return Number_Type;
function Compose(X : Number_Type) return Number_Type;
function Compose(X : Number_Type) return Number_Type is begin return F(G(X)); end;
type Function_Type is access function(X : Float) return Float;
function sqr(X : Float) return Float is begin return X * X; end;
package Math is new Ada.Numerics.Generic_Elementary_Functions(Float);
type Function_List is array(Natural range <>) of Function_Type;
functions : Function_List := (Math.sin'Access, Math.cos'Access, Program.sqr'Access);
inverts : Function_List of Function_Type := (Math.arcsin'Access, Math.arccos'Access, Math.sqrt'Access);
begin
for i in functions'Range loop
declare
function C is new Compose(Float, functions(i).all, inverts(i).all);
begin
Ada.Text_IO.Put_Line(Float'Image(C(0.5)));
end;
end loop;
end Program;


fcf()={
</lang>
my(A,B);
A=[x->sin(x), x->cos(x), x->x^2];
B=[x->asin(x), x->acos(x), x->sqrt(x)];
for(i=1,#A,
print(compose(A[i],B[i])(.5))
)
};</lang>
Usage note: In Pari/GP 2.4.3 the vectors can be written as
<lang parigp> A=[sin, cos, x->x^2];
B=[asin, acos, x->sqrt(x)];</lang>

Latest revision as of 20:38, 16 July 2011

<lang parigp>compose(f,g)={

 x -> f(g(x))

};

fcf()={

 my(A,B);
 A=[x->sin(x), x->cos(x), x->x^2];
 B=[x->asin(x), x->acos(x), x->sqrt(x)];
 for(i=1,#A,
   print(compose(A[i],B[i])(.5))
 )

};</lang> Usage note: In Pari/GP 2.4.3 the vectors can be written as <lang parigp> A=[sin, cos, x->x^2];

 B=[asin, acos, x->sqrt(x)];</lang>