why is octave such a crap?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

why is octave such a crap?

Frank Zack
function retval=octsux(x)
  if(x<1.3)                         % -inf<x<1.3
    retval=1/(exp(-x(1))+0.27);
  else                                 % 1.3<=x<inf
    retval=4/(3*sqrt(pi))*(x(1)^2+pi^2/6)^(3/4);
  endif  
endfunction

octsux(10)

warning: function name 'octsux' does not agree with function file name '/user/ME/test.m'
error: 'x' undefined near line 2 column 6
error: called from


wtf?

Reply | Threaded
Open this post in threaded view
|

function files (was: why is octave such a crap?)

Mike Miller-4
On Wed, Aug 17, 2016 at 14:50:30 +0000, Frank Zack wrote:

> function retval=octsux(x)
>   if(x<1.3)                         % -inf<x<1.3
>     retval=1/(exp(-x(1))+0.27);
>   else                                 % 1.3<=x<inf
>     retval=4/(3*sqrt(pi))*(x(1)^2+pi^2/6)^(3/4);
>   endif  
> endfunction
>
> octsux(10)
> warning: function name 'octsux' does not agree with function file name '/user/ME/test.m'
> error: 'x' undefined near line 2 column 6
> error: called from

Is the error message unclear? The function name must be the same as the
name of the .m file. A .m file may contain more than one subfunction,
but every public function must be in its own .m file with the same name.

https://www.gnu.org/software/octave/doc/interpreter/Function-Files.html

--
mike

Reply | Threaded
Open this post in threaded view
|

Re: why is octave such a crap?

Juan Pablo Carbajal-2
In reply to this post by Frank Zack
On Wed, Aug 17, 2016 at 4:50 PM, Frank Zack <[hidden email]> wrote:

> function retval=octsux(x)
>   if(x<1.3)                         % -inf<x<1.3
>     retval=1/(exp(-x(1))+0.27);
>   else                                 % 1.3<=x<inf
>     retval=4/(3*sqrt(pi))*(x(1)^2+pi^2/6)^(3/4);
>   endif
> endfunction
>
> octsux(10)
>
> warning: function name 'octsux' does not agree with function file name
> '/user/ME/test.m'
> error: 'x' undefined near line 2 column 6
> error: called from
>
>
> wtf?
>
I think you misspelled the subject of your mail:  "is octave" --> "am I".
Read the error message carefully and you will find the solution.

Reply | Threaded
Open this post in threaded view
|

Re: why is octave such a crap?

bpabbott
Administrator
In reply to this post by Frank Zack
On Aug 17, 2016, at 10:50, Frank Zack <[hidden email]> wrote:

function retval=octsux(x)
  if(x<1.3)                         % -inf<x<1.3
    retval=1/(exp(-x(1))+0.27);
  else                                 % 1.3<=x<inf
    retval=4/(3*sqrt(pi))*(x(1)^2+pi^2/6)^(3/4);
  endif  
endfunction

octsux(10)

warning: function name 'octsux' does not agree with function file name '/user/ME/test.m'
error: 'x' undefined near line 2 column 6
error: called from

wtf?

I assume you placed the example above in '/user/ME/test.m'?

The problem is that test.m is being parsed as a function and the parser is confused because the file name implies the function name is test() but the function declaration specifies octsux().

If the first line of your file is anything other than a comment or a function declaration it would work.

Try placing the text below in usux.m 

## start
1;
function retval=octsux(x)
  if(x<1.3)                         % -inf<x<1.3
    retval=1/(exp(-x(1))+0.27);
  else                                 % 1.3<=x<inf
    retval=4/(3*sqrt(pi))*(x(1)^2+pi^2/6)^(3/4);
  endif  
endfunction

octsux(10)
## end

And type 'usux' at octave's prompt

Ben ;-)
Reply | Threaded
Open this post in threaded view
|

Re: function files (was: why is octave such a crap?)

PhilipNienhuis
In reply to this post by Mike Miller-4
Mike Miller-4 wrote
On Wed, Aug 17, 2016 at 14:50:30 +0000, Frank Zack wrote:
> function retval=octsux(x)
>   if(x<1.3)                         % -inf<x<1.3
>     retval=1/(exp(-x(1))+0.27);
>   else                                 % 1.3<=x<inf
>     retval=4/(3*sqrt(pi))*(x(1)^2+pi^2/6)^(3/4);
>   endif  
> endfunction
>
> octsux(10)
> warning: function name 'octsux' does not agree with function file name '/user/ME/test.m'
> error: 'x' undefined near line 2 column 6
> error: called from

Is the error message unclear? The function name must be the same as the
name of the .m file. A .m file may contain more than one subfunction,
but every public function must be in its own .m file with the same name.

https://www.gnu.org/software/octave/doc/interpreter/Function-Files.html
IMO "crap" is unduly offensive but as far as Matlab compatibility goes the OP does have a point:

Matlab r2016b prerelease (the version I have at hand here) does accept function names that are different than the .m file name.
Matlab informs the user of this at file saving time rather than error'ing out at execution time.

Matlab seems to use the first function declaration in the file as entry point for the entire .m file.

Philip
Reply | Threaded
Open this post in threaded view
|

Re: function files (was: why is octave such a crap?)

NJank
On Wed, Aug 17, 2016 at 2:01 PM, PhilipNienhuis <[hidden email]> wrote:
Mike Miller-4 wrote
> On Wed, Aug 17, 2016 at 14:50:30 +0000, Frank Zack wrote:

Matlab r2016b prerelease (the version I have at hand here) does accept
function names that are different than the .m file name.
Matlab informs the user of this at file saving time rather than error'ing
out at execution time.

last I checked, Octave did the same thing.  Notice that the function name message is a warning, not an error. I've renamed functions and forgotten to change the filename, and it just pops up a warning. I'm not actually sure why it errors on the x definition here, unless that behavior is new.
Reply | Threaded
Open this post in threaded view
|

Re: function files (was: why is octave such a crap?)

NJank
On Wed, Aug 17, 2016 at 2:06 PM, Nicholas Jankowski <[hidden email]> wrote:
On Wed, Aug 17, 2016 at 2:01 PM, PhilipNienhuis <[hidden email]> wrote:
Mike Miller-4 wrote
> On Wed, Aug 17, 2016 at 14:50:30 +0000, Frank Zack wrote:

Matlab r2016b prerelease (the version I have at hand here) does accept
function names that are different than the .m file name.
Matlab informs the user of this at file saving time rather than error'ing
out at execution time.

last I checked, Octave did the same thing.  Notice that the function name message is a warning, not an error. I've renamed functions and forgotten to change the filename, and it just pops up a warning. I'm not actually sure why it errors on the x definition here, unless that behavior is new.

running 4.0.3 on Windows. Sure enough I just took one of my function files and renamed the function without changing the filename.  got the same warning, then it gave me the correct output without error.  So I think we're okay there.

nickj
Reply | Threaded
Open this post in threaded view
|

Re: function files (was: why is octave such a crap?)

Mike Miller-4
On Wed, Aug 17, 2016 at 14:10:42 -0400, Nicholas Jankowski wrote:

> On Wed, Aug 17, 2016 at 2:06 PM, Nicholas Jankowski <[hidden email]>
> wrote:
>
> > On Wed, Aug 17, 2016 at 2:01 PM, PhilipNienhuis <[hidden email]>
> > wrote:
> >
> >> Mike Miller-4 wrote
> >> > On Wed, Aug 17, 2016 at 14:50:30 +0000, Frank Zack wrote:
> >>
> >> Matlab r2016b prerelease (the version I have at hand here) does accept
> >> function names that are different than the .m file name.
> >> Matlab informs the user of this at file saving time rather than error'ing
> >> out at execution time.
> >>
> >
> > last I checked, Octave did the same thing.  Notice that the function name
> > message is a warning, not an error. I've renamed functions and forgotten to
> > change the filename, and it just pops up a warning. I'm not actually sure
> > why it errors on the x definition here, unless that behavior is new.
> >
>
> running 4.0.3 on Windows. Sure enough I just took one of my function files
> and renamed the function without changing the filename.  got the same
> warning, then it gave me the correct output without error.  So I think
> we're okay there.

So to be clear, you are calling the name of the file as if it were the
function name, and it works, right? For example if I have myfunc.m with

  function y = squared (x)
    y = x.*x;
  endfunction

You can call myfunc(2) and get back 4, but you cannot call squared(2)
because Octave has no way of knowing where that function is defined if
it's not the same as the file name.

The original post showed an attempt to call a function where the name
matched the function declaration in the file, but *not* the name of the
file. This should have thrown a simple "'foo' undefined" error.

I suspect that what the OP actually did was call "test" without any
arguments, as if it were a script file, not a function file. If I do any
of the following with myfunc, I get the same error as the OP:

  >> myfunc
  error: 'x' undefined near line 2 column 7
  error: called from
      myfunc at line 2 column 5
  >> run myfunc
  'x' undefined near line 2 column 7
  error: called from
      run at line 86 column 5
  >> source myfunc.m
  error: 'x' undefined near line 2 column 7
  error: called from
      myfunc at line 2 column 5

I think the confusion point for the OP was the difference between a
script file and a function file.

--
mike

Reply | Threaded
Open this post in threaded view
|

Re: function files

Daniel Sebald
On 08/17/2016 01:25 PM, Mike Miller wrote:

> On Wed, Aug 17, 2016 at 14:10:42 -0400, Nicholas Jankowski wrote:
>> On Wed, Aug 17, 2016 at 2:06 PM, Nicholas Jankowski <[hidden email]>
>> wrote:
>>
>>> On Wed, Aug 17, 2016 at 2:01 PM, PhilipNienhuis <[hidden email]>
>>> wrote:
>>>
>>>> Mike Miller-4 wrote
>>>>> On Wed, Aug 17, 2016 at 14:50:30 +0000, Frank Zack wrote:
>>>>
>>>> Matlab r2016b prerelease (the version I have at hand here) does accept
>>>> function names that are different than the .m file name.
>>>> Matlab informs the user of this at file saving time rather than error'ing
>>>> out at execution time.
>>>>
>>>
>>> last I checked, Octave did the same thing.  Notice that the function name
>>> message is a warning, not an error. I've renamed functions and forgotten to
>>> change the filename, and it just pops up a warning. I'm not actually sure
>>> why it errors on the x definition here, unless that behavior is new.
>>>
>>
>> running 4.0.3 on Windows. Sure enough I just took one of my function files
>> and renamed the function without changing the filename.  got the same
>> warning, then it gave me the correct output without error.  So I think
>> we're okay there.
>
> So to be clear, you are calling the name of the file as if it were the
> function name, and it works, right? For example if I have myfunc.m with
>
>    function y = squared (x)
>      y = x.*x;
>    endfunction
>
> You can call myfunc(2) and get back 4, but you cannot call squared(2)
> because Octave has no way of knowing where that function is defined if
> it's not the same as the file name.
>
> The original post showed an attempt to call a function where the name
> matched the function declaration in the file, but *not* the name of the
> file. This should have thrown a simple "'foo' undefined" error.
>
> I suspect that what the OP actually did was call "test" without any
> arguments, as if it were a script file, not a function file. If I do any
> of the following with myfunc, I get the same error as the OP:
>
>    >> myfunc
>    error: 'x' undefined near line 2 column 7
>    error: called from
>        myfunc at line 2 column 5
>    >> run myfunc
>    'x' undefined near line 2 column 7
>    error: called from
>        run at line 86 column 5
>    >> source myfunc.m
>    error: 'x' undefined near line 2 column 7
>    error: called from
>        myfunc at line 2 column 5
>
> I think the confusion point for the OP was the difference between a
> script file and a function file.

This type of confusion is related to the following

https://savannah.gnu.org/bugs/?46852

i.e., something in the symbol table overrides a prior meaning.  There
are a lot of existing functions, and many of them are names one would
typically pick when constructing a program.

Dan