Adept Scientific - English
The world's best software and hardware for research, science and engineering.
flag arrow
clearclear

 Adept Store | register Join My Adept | Flags  
Adept Scientific | Amor Way | Letchworth Garden City | Herts | SG6 1ZA | Tel: +44 (0)1462 480055  
UKdedksvnofi
Home
Products
Training
Events
 Buy Online
Downloads
Academic Discounts
Support
My Adept
International |  About Us |  Adept Scientific Blog |  Contact Us |  Press Room |  Jobs
Adept Scientific on Facebook Adept Scientific on Twitter Adept Scientific on YouBube Adept Scientific on LinkedIn


The Next Steps

• Ask us a question
• Watch Maple Video Demonstrations
• Buy Maple Now
• View Maple Pricing
• Download a Brochure
• Request a Brochure
• Request an Evaluation
• Meet Our Team
• Read our RSS Feeds

Learn More

Maple Home
Maple 16 Overview
Maple 16 Professional
Maple 16 Academic
Maple 16 Student Use
What's New in Maple 16
Maple New Features
Datasheet

Maple History
Recorded Online Seminars

MapleSim
MapleNet
Maple T.A.
BlockImporter™
Maple Toolboxes

Maple Rave Reviews
Maple Study Guides
Books about Maple
System Requirements

Latest Information

New Features: Professional
New Features: Academic
Maple Features
The Maple Reporter Online

Service & Support

Maple Primes
blogs, forums etc

Elite Maintenance Program
Application Centre
Powertools
Search the Knowledge Base
Technical Support request

List Archives >  Maple User Group List Archive >  Archive by date >  This Month By Date >  This Month By Topic

[MUG] Nargs

Search email archive for  

[MUG] Nargs
Author: PierLuigi Zezza    Posted: Mon, 16 Dec 2002 22:36:45 +0100

>> From: PierLuigi Zezza />
I am constructing (Maple >= 5 release 5) a procedure which has a function
(procedure) as an input and I want to detect the number of arguments of the
input.
I tried with
> myproc:=proc(f::procedure) nops({op(1,eval(f))}) end;

myproc := proc(f::procedure) nops({op(1, eval(f))}) end proc

so that
> f:=x->x^2-1;

2
f := x -> x - 1

> myproc(f);

1

> F:=(x,y)->x^2;

2
F := (x, y) -> x

> myproc(F);

2

but I would like to have has input also linear combinations or composition
of functions as
> g:=x->(x+1);

g := x -> x + 1

> (2*f-g)(x);

2
2 x - 3 - x

and
> h:=t->(t^2,t);

2
h := t -> (t , t)

> (f@h)(t);

4
t - 1

but my procedure does not work and I think that the reason is that
> type(2*f-g,procedure);

false

> type(f@h,procedure);

false

and hence
> op(1,eval(f@h));

f

> op(1,eval(2*f-g));

2 f

while
> op(1,eval(F));

x, y

any suggestion is welcome
gigi zezza

PierLuigi Zezza
/>

[MUG] Re: Nargs
Author: Maple User Group    Posted: Fri, 20 Dec 2002 14:18:17 -0500

>> From: Maple User Group />
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-

Date: Tue, 17 Dec 2002 15:33:32 -0500 (EST)
From: Carl Devore /> To: /> Subject: Nargs



On Mon, 16 Dec 2002, PierLuigi Zezza wrote:
> I am constructing (Maple >= 5 release 5) a procedure which has a function
> (procedure) as an input and I want to detect the number of arguments of the
> input.

Correction: You want to detect the number of parameters, not the number of
arguments. Procedures are defined with parameters and called with
arguments. Counting the arguments is trivial.

> I tried with
> > myproc:=proc(f::procedure) nops({op(1,eval(f))}) end;
>
> but I would like to have has input also linear combinations or composition
> of functions as
> > g:=x->(x+1);
> > (2*f-g)(x);
> > h:=t->(t^2,t);
> > (f@h)(t);
> but my procedure does not work and I think that the reason is that
> > type(2*f-g,procedure);
> false

Yes, that is the reason. I cannot come up with a simple type expression
that will cover all the cases. Maybe someone else can. Nonetheless, I
think that the following will cover the vast majority of cases, certainly
all your cases above.

Nparams:= proc(f)
local F,1,x;
F:= indets(f, `@`);
if F<>{} then return Nparams(op(-1, F[1])) fi;
F:= indets(f, procedure);
F1:= remove(x-> member(_syslib, {attributes(x)}, F);
if F1<>{} then nops([op(1, eval(F1[1]))])
elif F<>{} then nops([op(1, eval(F[1]))])
else 0
fi
end proc;

Since many Maple procedures like arctan and abs are weird cases with
mulitple parameters, I restrict the count, if possible, to the
user-defined procedures. But my procedure might still give unexpected
results for something like />

-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-

From: "Dr Francis J. Wright" /> To: /> Subject: Nargs
Date: Wed, 18 Dec 2002 17:37:20 -0000

Generalize the type test to accept expressions of whatever form you want.
If the procedure is called with an argument that is not explicitly of type
procedure then analyse the expression to find its procedural components and
check their argument numbers: with a composition you presumably care only
about the rightmost procedure; with a sum or product you presumably want the
minimum number of arguments, etc. You might also want to think about how to
handle a purely symbolic function, i.e. a symbol to which no procedure has
been assigned, which you might want to trap as an error.

Francis


-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-

Date: Wed, 18 Dec 2002 21:21:25 -0500 (EST)
From: Stephen Forrest /> To: /> Subject: Nargs


Right. Your procedure will have to check the type of the arguments, and
handle these cases differently. In the case of the composition @, it's
obviously the number of arguments of the innermost procedure you want, so
recurse onto that.

In the case of a sum or product of procedures, it's probably the maximum
of all numbers of arguments of each of the subprocedures that you want.

However, for whatever expression you invent, it will probably be possible
to come up with a counterexample of something acts like a procedure but
isn't handled properly. And you will never be able to handle cases that
manipulate args and nargs directly in the procedure body, like

p := proc() if nargs>1 then args[2] elsif nargs>0 args[1] else 0 fi; end:

except as very special cases. So I'm afraid don't really understand why
you'd like to do this.

Steve


-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-

From: "Greg Gamble" /> To: /> Subject: Nargs
Date: Thu, 19 Dec 2002 12:21:28 +0800

There are two things to consider here:
* the *actual* number of arguments that a function is *called* with,
(this is dynamically assigned to the variable `nargs' when a function/
procedure is executed);
* the number of *named arguments* the function is *defined* with
(for want of a better name let's call this the Arity of a function,
since functions may be unary = 1-ary, binary = 2-ary, ternary = 3-ary
or, in general, n-ary)

Suppose we define f as follows:

> f := (x, y) -> (x, x^2, x^3);
2 3
f := (x, y) -> (x, x , x )

> f(1, 2);
1, 1, 1

> f(1);
1, 1, 1

> f(1, 2, 3);
1, 1, 1

As you can see, despite the fact that f was defined with 2 arguments
(so its Arity is 2), Maple is happy to execute f with any number of
arguments,
so long as it can evaluate it. Here y is not needed in the body of the
function,
so Maple does not object to f being called with 1 argument. By not checking
the number of arguments, Maple allows for the possibility of a variable
number of arguments or optional arguments. Let's define g to be functionally
equivalent
to f via proc so that we can report via `nargs' the number of arguments its
called with:

> g := proc(x, y) printf("nargs: %a\n", nargs); (x, x^2, x^3); end;
g := proc(x, y) printf("nargs: %a\n", nargs); x, x^2, x^3 end proc

> g(1, 2);
nargs: 2
1, 1, 1

> g(1);
nargs: 1
1, 1, 1

> g(1, 2, 3);
nargs: 3
1, 1, 1
Now consider:

> h := proc(x)
> local y;
> if nargs > 1 then
> y := args[2];
> fi;
> (x, x^2, x^3);
> end;
h := proc(x)
local y;
if 1 < nargs then y := args[2] end if; x, x^2, x^3
end proc

Again, h is equivalent to f but the (useless) y is an *optional argument*
that is local to the body of the function, i.e. by my definition y is not
a *named argument*: the Arity of h is 1, whereas the Arity of f or g is 2.
This makes the concept of Arity a bit nebulous in Maple.


You were hoping that Maple would somehow reduce linear combinations and
compositions to an arrow operator form which Maple doesn't do, so eval(f@h)
is just f@h, and there are other considerations e.g. Maple treats constants
as functions: 3(x, y, z) is 3. Your myproc is a rudimentary Arity but you do
not want to enforce the type `procedure' on its argument (as you
discovered).
Here's maybe something close to what you want:

> Arity := f ->
> if type(f, constant) then
> 0
> elif type(f, procedure) then
> nops({op(1, eval(f))})
> elif type(f, `@`) then
> Arity(op(2, f))
> elif type(f, {`+`, `*`}) then
> max(seq(Arity(i), i = [op(f)]))
> else
> error "arity unknown for this function type"
> fi;

Arity := proc(f)
option operator, arrow;
if type(f, constant) then 0
elif type(f, procedure) then nops({op(1, eval(f))})
elif type(f, `@`) then Arity(op(2, f))
elif type(f, {`*`, `+`}) then
max(seq(Arity(i), i = [op(f)]))
else error "arity unknown for this function type"
end if
end proc

Features of (the above) Arity:
1. Arity(f + g) = max(Arity(f), Arity(g))
2. Arity(f * g) = max(Arity(f), Arity(g))
3. Arity(f@g) = Arity(g)

My guess is that you actually want to test well-defined-ness as well i.e.
you want:

1. Arity(f + g) = Arity(f) if Arity(f) = Arity(g)
2. Arity(f * g) = Arity(g) if 0 <> Arity(f) or Arity(f) = Arity(g)
3. Arity(f@g) = Arity(g) if Arity(f) = ArityOfInverse(g)

where ArityOfInverse is defined by:

> ArityOfInverse := f -> nops([f(x['i'] $ 'i' = 1 .. Arity(f))]);

ArityOfInverse := f -> nops([f(x['i'] $ ('i' = 1 .. Arity(f)))])

Anyway, I hope those ideas are helpful.

Regards,
Greg Gamble

// Greg Gamble, Dept. of Maths & Stats //
// Curtin University, Bentley WA 6102 //
// Mailto: //
// WWW: http://www.maths.curtin.edu.au/~gregg //


Previous by date: [MUG] dsolve does not find second solution, Helmut Kahovec
Next by date: [MUG] how to implicitplot this func, Ruan Zhichao
Previous thread: [MUG] Evaluation problem, Colin Campbell - IST
Next thread: [MUG] how to implicitplot this func, Ruan Zhichao



Ready to buy?

For more pricing information:
Visit our webstore, call us on +1 800 724 8380 or email us at info@adeptscience.com

Featured Downloads

Maple 16 & MapleSim 5 Professional Brochure
Maple 16 Academic Datasheet
Maple 16 & MapleSim 5 Academic Brochure
Maple 16 What is New datasheet
Maple 16 Professional Datasheet
Maple Whitepaper: Driving Innovation - How mathematical modeling and optimisation increase efficiency and productivity in vehicle design.
MapleSim Whitepaper - Technological Superiority in Multi-Domain Physical Modelling and Simulation

Latest Downloads

Maple 16 Programming Guide
Maple 16 User Manual
Maple 16 Academic Datasheet
Maple 16 Professional Datasheet
Maple 16 & MapleSim 5 Academic Brochure

Product Reviews

"Without the Maple software, we would have to spend weeks generating the equations of motion for every experiment. Then the chances that we did it right would basically be near zero. There would always be a mistake somewhere. It is very difficult to set up a dynamic motion model by hand."
- Jean-Claude PiedBeouf, Ph.D Manager of Robotics, Canadian Space Agency

"Its very good - highly accurate and easy to use. The speed of Maple allows me to change equations and quickly reintegrate them into the application, so more possibilities can be explored to achieve the precise effect desired."
Shawn Neely, Senior R & D Director for PDI/Dreamworks

Latest News

Connectivity to major CAD systems extended in Maple 16
MapleSim Breaks New Ground in Hardware-in-the-Loop real-time simulation for planetary rovers
MapleSim Breaks New Ground in Hardware-in-the-Loop real-time simulation for planetary rovers
Maths software usability reaches new heights with Maple 16
"MapleSim was an eye-opener for us.
adept

Top of the Page

Popular Links: ChemDraw | ChemOffice | Data Acquisition | Data Analysis | EndNote | Maple | MapleSim | Mathcad | MathType | Quality Analyst | Reference Manager | VisSim

EU ePrivacy Directive | Our Privacy and Terms and Conditions Statement
All Trademarks Recognised. Copyright © 2012, Adept Scientific plc.
Site designed and maintained by Lyndon Ash

Adept Scientific | Amor Way | Letchworth Garden City | Herts | SG6 1ZA | Tel: +44 (0)1462 480055