inline functions in Matlab R14

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

inline functions in Matlab R14

John W. Eaton-6
Can someone who has Matlab R14 say what it does in the following
cases?

  inline ('p_1+1')

  inline ('p+q')

  inline ('abc+xyz')

Thanks,

jwe


Reply | Threaded
Open this post in threaded view
|

Re: inline functions in Matlab R14

David Bateman-3
According to John W. Eaton <[hidden email]> (on 09/22/04):
> Can someone who has Matlab R14 say what it does in the following
> cases?
>
>   inline ('p_1+1')
>
>   inline ('p+q')
>
>   inline ('abc+xyz')

Could I also suggest the case


   inline ('Abc+xyZ')

as the documentation is seems to imply special treatment for upper and
lower case characters...

D.

--
David Bateman                                [hidden email]
Motorola CRM                                 +33 1 69 35 48 04 (Ph)
Parc Les Algorithmes, Commune de St Aubin    +33 1 69 35 77 01 (Fax)
91193 Gif-Sur-Yvette FRANCE

The information contained in this communication has been classified as:

[x] General Business Information
[ ] Motorola Internal Use Only
[ ] Motorola Confidential Proprietary


Reply | Threaded
Open this post in threaded view
|

RE: inline functions in Matlab R14

HALL, BENJAMIN            PW
In reply to this post by John W. Eaton-6
>> inline('p_1+1')

ans =

     Inline function:
     ans(p_1) = p_1+1

>> inline('p+q')

ans =

     Inline function:
     ans(p,q) = p+q

>> inline('abc+xyz')

ans =

     Inline function:
     ans(abc,xyz) = abc+xyz



-----Original Message-----
From: John W. Eaton [mailto:[hidden email]]
Sent: Wednesday, September 22, 2004 1:30 PM
To: octave maintainers mailing list
Subject: inline functions in Matlab R14


Can someone who has Matlab R14 say what it does in the following
cases?

  inline ('p_1+1')

  inline ('p+q')

  inline ('abc+xyz')

Thanks,

jwe


Reply | Threaded
Open this post in threaded view
|

RE: inline functions in Matlab R14

HALL, BENJAMIN            PW
In reply to this post by John W. Eaton-6
Still seems to get it right.  Matlab indicates they're using a function
called "symvar" to figure out what are the variables.


>> inline('Abc+xyZ')

ans =

     Inline function:
     ans(Abc,xyZ) = Abc+xyZ



-----Original Message-----
From: David Bateman [mailto:[hidden email]]
Sent: Wednesday, September 22, 2004 12:36 PM
To: John W. Eaton
Cc: octave maintainers mailing list
Subject: Re: inline functions in Matlab R14


According to John W. Eaton <[hidden email]> (on 09/22/04):
> Can someone who has Matlab R14 say what it does in the following
> cases?
>
>   inline ('p_1+1')
>
>   inline ('p+q')
>
>   inline ('abc+xyz')

Could I also suggest the case


   inline ('Abc+xyZ')

as the documentation is seems to imply special treatment for upper and
lower case characters...

D.

--
David Bateman                                [hidden email]
Motorola CRM                                 +33 1 69 35 48 04 (Ph)
Parc Les Algorithmes, Commune de St Aubin    +33 1 69 35 77 01 (Fax)
91193 Gif-Sur-Yvette FRANCE

The information contained in this communication has been classified as:

[x] General Business Information
[ ] Motorola Internal Use Only
[ ] Motorola Confidential Proprietary


Reply | Threaded
Open this post in threaded view
|

Re: inline functions in Matlab R14

Philipp.Batchelor
In reply to this post by John W. Eaton-6
John W. Eaton wrote:

> Can someone who has Matlab R14 say what it does in the following
> cases?
>
>   inline ('p_1+1')
>
>   inline ('p+q')
>
>   inline ('abc+xyz')
>
> Thanks,
>
> jwe
>


Here is what I get:(seems meaningful): I removed some empty lines:
'p_1+1' is a function of one variable, others of two variables
(complains with 1 input).

-------------------------------
 >> version
ans =
7.0.0.19901 (R14)
 >> inline ('p_1+1')
ans =
      Inline function:
      ans(p_1) = p_1+1

 >>   inline ('p+q')
ans =
      Inline function:
      ans(p,q) = p+q

 >>   inline ('abc+xyz')
ans =
      Inline function:
      ans(abc,xyz) = abc+xyz

% and testing:

 >> g = inline ('p_1+1')
g =
      Inline function:
      g(p_1) = p_1+1

 >> g(1)
ans =
      2

 >> g(2)
ans =
      3

 >> k = inline ('p+q')
k =
      Inline function:
      k(p,q) = p+q
 >> k(1,1), k(pi,exp(1))
ans =
      2
ans =
     5.8599

 >> h  = inline ('abc+xyz')
h =
      Inline function:
      h(abc,xyz) = abc+xyz

 >> h(1,1)
ans =
      2

 >> h(1,2)
ans =
      3
 >> l = inline ('Abc+xyZ')

l =

      Inline function:
      l(Abc,xyZ) = Abc+xyZ

 >> l(1,pi)

ans =

     4.1416

% and for vector inputs:

 >> g([1,1])

ans =

      2     2

 >> l([1,1],2)

ans =

      3     3


Reply | Threaded
Open this post in threaded view
|

Re: inline functions in Matlab R14

Quentin Spencer
In reply to this post by David Bateman-3
Here's the output I get:

                              < M A T L A B >
                  Copyright 1984-2004 The MathWorks, Inc.
                         Version 7.0.0.19901 (R14)
                               May 06, 2004


  To get started, type one of these: helpwin, helpdesk, or demo.
  For product information, visit www.mathworks.com.

 >>
 >> inline ('p_1+1')
ans =
     Inline function:
     ans(p_1) = p_1+1
 >> inline ('p+q')
ans =
     Inline function:
     ans(p,q) = p+q
 >> inline ('abc+xyz')
ans =
     Inline function:
     ans(abc,xyz) = abc+xyz
 >> inline ('Abc+xyZ')
ans =
     Inline function:
     ans(Abc,xyZ) = Abc+xyZ




David Bateman wrote:

>According to John W. Eaton <[hidden email]> (on 09/22/04):
>  
>
>>Can someone who has Matlab R14 say what it does in the following
>>cases?
>>
>>  inline ('p_1+1')
>>
>>  inline ('p+q')
>>
>>  inline ('abc+xyz')
>>    
>>
>
>Could I also suggest the case
>
>
>   inline ('Abc+xyZ')
>
>as the documentation is seems to imply special treatment for upper and
>lower case characters...
>
>D.
>
>  
>


Reply | Threaded
Open this post in threaded view
|

RE: inline functions in Matlab R14

John W. Eaton-6
In reply to this post by HALL, BENJAMIN PW
On 22-Sep-2004, Hall, Benjamin <[hidden email]> wrote:

| Still seems to get it right.

For some value of "right".  The observed behavior does not seem to
match my reading of the docs.

jwe


Reply | Threaded
Open this post in threaded view
|

Re: inline functions in Matlab R14

David Bateman-3
In reply to this post by Quentin Spencer

Even worse, what do

inline('sin (x)')

and

inline('y (x)')

give.. I suspect they give

ans =

     Inline function:
     f(x) = sin (x)

and

ans =

     Inline function:
     f(x) = y (x)

since how can you tell if "sin" and "y" are functions or not. Even if they
aren't when you define the inline function they might be afterwards...

Another ambiguity is

inline('x+i'), inline('x+j'), inline('x+I'), inline('x+J')

the first two should have i and j ignored. But matlab doesn't define
I and J. Also what about the octave built in constants "e" and "E"?
Should they also be ignored...

I also noticed that matlab sorts the arguments in ascii order. So that

inline('a+Y') ->

f =

     Inline function:
     f(Y,a) = a+Y

Grrr, this is a case where matlabs documentation doesn't even closely
resemble what they actually do.

The attached patch tries to do something similar to the matlab behaviour.
I ignore the octave builtin constants "I", "J", "e" and "E"... John, its
up to you if you also want to drop these as potential inline function
arguments...

D.

2004-09-23 David Bateman <[hidden email]>

        * ov-fcn-inline.cc (Finline): Do what the other brand actually
        does, rather than what they say they do.


According to Quentin Spencer <[hidden email]> (on 09/22/04):

> Here's the output I get:
>
>                              < M A T L A B >
>                  Copyright 1984-2004 The MathWorks, Inc.
>                         Version 7.0.0.19901 (R14)
>                               May 06, 2004
>
>
>  To get started, type one of these: helpwin, helpdesk, or demo.
>  For product information, visit www.mathworks.com.
>
> >>
> >> inline ('p_1+1')
> ans =
>     Inline function:
>     ans(p_1) = p_1+1
> >> inline ('p+q')
> ans =
>     Inline function:
>     ans(p,q) = p+q
> >> inline ('abc+xyz')
> ans =
>     Inline function:
>     ans(abc,xyz) = abc+xyz
> >> inline ('Abc+xyZ')
> ans =
>     Inline function:
>     ans(Abc,xyZ) = Abc+xyZ
>
>
>
>
> David Bateman wrote:
>
> >According to John W. Eaton <[hidden email]> (on 09/22/04):
> >
> >
> >>Can someone who has Matlab R14 say what it does in the following
> >>cases?
> >>
> >> inline ('p_1+1')
> >>
> >> inline ('p+q')
> >>
> >> inline ('abc+xyz')
> >>  
> >>
> >
> >Could I also suggest the case
> >
> >
> >  inline ('Abc+xyZ')
> >
> >as the documentation is seems to imply special treatment for upper and
> >lower case characters...
> >
> >D.
> >
> >
> >
--
David Bateman                                [hidden email]
Motorola CRM                                 +33 1 69 35 48 04 (Ph)
Parc Les Algorithmes, Commune de St Aubin    +33 1 69 35 77 01 (Fax)
91193 Gif-Sur-Yvette FRANCE

The information contained in this communication has been classified as:

[x] General Business Information
[ ] Motorola Internal Use Only
[ ] Motorola Confidential Proprietary

patch.sort20040923 (3K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: inline functions in Matlab R14

John W. Eaton-6
On 23-Sep-2004, David Bateman <[hidden email]> wrote:

| Even worse, what do
|
| inline('sin (x)')
|
| and
|
| inline('y (x)')
|
| give.. I suspect they give
|
| ans =
|
|      Inline function:
|      f(x) = sin (x)
|
| and
|
| ans =
|
|      Inline function:
|      f(x) = y (x)
|
| since how can you tell if "sin" and "y" are functions or not. Even if they
| aren't when you define the inline function they might be afterwards...

In Matlab, that is not possible because all functions are either
built-in or defined in a .m file somewhere in your path.  Although I
suppose you could modify the path later and a new function would be
available.  In that case, I don't think it would go back and alter the
affected inline functions, but if you used the same inline again, this
time with the external function(s) available, I would expect it to
change the meaning.  Once again, bad design.  Is it surprising?

| Another ambiguity is
|
| inline('x+i'), inline('x+j'), inline('x+I'), inline('x+J')
|
| the first two should have i and j ignored. But matlab doesn't define
| I and J. Also what about the octave built in constants "e" and "E"?
| Should they also be ignored...

Ugh.

| The attached patch tries to do something similar to the matlab behaviour.
| I ignore the octave builtin constants "I", "J", "e" and "E"... John, its
| up to you if you also want to drop these as potential inline function
| arguments...

OK, I applied the patch (sorry I missed it yesterday).  Unfortunately,
it does not really recognize functions correctly.  What if you do

  inline ('array_variable(2)')

(i.e., you are defining a function to extract elements from an
array).  The current version of inline does this:

  octave:1> inline ('array_variable (2)')
  ans =

  f(array) = array_variable (2)

  octave:2> inline ('arrayvar (2)')
  ans =

  f() = arrayvar (2)

In both cases, Matlab apparently does

  f(x) = arrayvar (2)

which also seems wrong.  I have no function called arrayvar defined,
so it should be recognized as an argument to the function.  So even
Matlab is not actually looking at what functions are available.  It is
just using some lame half-parsing rules.  I don't think that can
possibly work in all cases.  BTW, it appears that your code will
recognize variable names inside character strings.

Also, what about functions that do not expect arguments.  In Matlab,
you are not allowed to write

  eye ()

to call a function, so you must write the name alone.  In that case,
you might write a wrapper function as inline like this:

  inline ('eye + 3')

but again, the current version of inline recognizes 'eye" as a
variable here.  Oh well, Matlab seems to do the same.

So the only real "bugs" that I see in the current code are:

  * It doesn't recognize "_" in symbol names.  Matlab R13 does not
    recognize leading underscores, but does recognize embedded
    underscores.  Perhaps that is a bug?

  * It doesn't handle things like

      inline ('foo (''bar baz'', x, y, z)')

    correctly -- the only args should be x, y, and z.

  * It doesn't use "x" as the default variable name, passed when no
    other variables are recognized (why it does this, I have no idea).

Finally, fixing these things is not all that important to me.  I'd
file this in the category of bug-for-bug compatibility.  I think
the only safe thing to do is always specify the arguments explicitly.  

jwe


Reply | Threaded
Open this post in threaded view
|

Re: inline functions in Matlab R14

David Bateman-3
According to John W. Eaton <[hidden email]> (on 09/24/04):

> | since how can you tell if "sin" and "y" are functions or not. Even if they
> | aren't when you define the inline function they might be afterwards...
>
> In Matlab, that is not possible because all functions are either
> built-in or defined in a .m file somewhere in your path.  Although I
> suppose you could modify the path later and a new function would be
> available.  In that case, I don't think it would go back and alter the
> affected inline functions, but if you used the same inline again, this
> time with the external function(s) available, I would expect it to
> change the meaning.  Once again, bad design.  Is it surprising?
Agreed..

> | Another ambiguity is
> |
> | inline('x+i'), inline('x+j'), inline('x+I'), inline('x+J')
> |
> | the first two should have i and j ignored. But matlab doesn't define
> | I and J. Also what about the octave built in constants "e" and "E"?
> | Should they also be ignored...
>
> Ugh.

Completely agreed, so should we also ignore [IJeE] ?

> | The attached patch tries to do something similar to the matlab behaviour.
> | I ignore the octave builtin constants "I", "J", "e" and "E"... John, its
> | up to you if you also want to drop these as potential inline function
> | arguments...
>
> OK, I applied the patch (sorry I missed it yesterday).  Unfortunately,
> it does not really recognize functions correctly.  What if you do
>
>   inline ('array_variable(2)')

Yes, that is what I was trying to get at with inline('x(y)'), where
matlab returns

 ans =
 
      Inline function:
      f(x) = y (x)
 
It seems that in fact even the inline version in R12 returns the same
behaviour as R14, so it is unforgivable that Matlab's doc are so out of
date for inline.

In any case, matlab assumes that [_a-zA-Z][_a-zA-Z0-9]*[\S]*( is a
function always... It doesn't seem to check the symbol table at all
For example

>> x = [1:3];
>> f = inline('x(y)')

f =

     Inline function:
     f(y) = x(y)

>> f(1)
??? Error using ==> inlineeval
Error in inline expression ==> x(y)
??? Undefined function or variable 'x'.

Error in ==> /crm/pacome/Matlab6.1/toolbox/matlab/funfun/@inline/subsref.m
On line 25  ==>     INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);


> (i.e., you are defining a function to extract elements from an
> array).  The current version of inline does this:
>
>   octave:1> inline ('array_variable (2)')
>   ans =
>
>   f(array) = array_variable (2)
>
>   octave:2> inline ('arrayvar (2)')
>   ans =
>
>   f() = arrayvar (2)
>
> In both cases, Matlab apparently does
>
>   f(x) = arrayvar (2)
>
> which also seems wrong.  I have no function called arrayvar defined,
> so it should be recognized as an argument to the function.  So even
> Matlab is not actually looking at what functions are available.  It is
> just using some lame half-parsing rules.  I don't think that can
> possibly work in all cases.  BTW, it appears that your code will
> recognize variable names inside character strings.

If arrayvar isn't available in the inline function, then it can't be
evaluated with subsref in any case, so it has to be passed as an
argument for this to make sense. Yes

f() = arrayvar (2)

is a bug however, but pretty stupid as it means there are no args to
the inline function...

> Also, what about functions that do not expect arguments.  In Matlab,
> you are not allowed to write
>
>   eye ()
>
> to call a function, so you must write the name alone.  In that case,
> you might write a wrapper function as inline like this:
>
>   inline ('eye + 3')
>
> but again, the current version of inline recognizes 'eye" as a
> variable here.  Oh well, Matlab seems to do the same.
>
> So the only real "bugs" that I see in the current code are:
>
>   * It doesn't recognize "_" in symbol names.  Matlab R13 does not
>     recognize leading underscores, but does recognize embedded
>     underscores.  Perhaps that is a bug?
ok, this is a bug..

                  if (! isalpha (c) || c == '_')

should read

                  if (! isalpha (c) && c != '_')

I also missed a trailing digit on the last arg, so that inline('a+x1')
wouldn't work either...

>   * It doesn't handle things like
>
>       inline ('foo (''bar baz'', x, y, z)')
>
>     correctly -- the only args should be x, y, and z.

Didn't think of that one... Grrrr... Ok, think for that and the aboved
attached.

>   * It doesn't use "x" as the default variable name, passed when no
>     other variables are recognized (why it does this, I have no idea).

Yes I thought about, this but then thought it wasn't worth treating
as if there are no args, whats the point of defining an arbitrary one..

> Finally, fixing these things is not all that important to me.  I'd
> file this in the category of bug-for-bug compatibility.  I think
> the only safe thing to do is always specify the arguments explicitly.  

I promise I'll leave it alone after this.. Its taken way too much time...

D.

--
David Bateman                                [hidden email]
Motorola CRM                                 +33 1 69 35 48 04 (Ph)
Parc Les Algorithmes, Commune de St Aubin    +33 1 69 35 77 01 (Fax)
91193 Gif-Sur-Yvette FRANCE

The information contained in this communication has been classified as:

[x] General Business Information
[ ] Motorola Internal Use Only
[ ] Motorola Confidential Proprietary

patch.sort20040924 (3K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: inline functions in Matlab R14

John W. Eaton-6
On 24-Sep-2004, David Bateman <[hidden email]> wrote:

| Completely agreed, so should we also ignore [IJeE] ?

I'd say let's not bother.

| I promise I'll leave it alone after this.. Its taken way too much time...

OK.  Thanks for the patch.  I've applied it.

I'll be making the 2.1.60 snapshot soon, so if there are any other
problems, please let me know as soon as possible.

jwe