Constants in functions?

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

Constants in functions?

Windhorn, Allen E [ACIM/LSA/MKT]
What's the accepted way to define a constant inside a function?  Example:
____________________________________
function [sc, err] = symmcomp(abc)
  % Function accepts [abc] complex components and delivers symmetrical
  % components as a vector [x0, x1, x2] of complex values
  persistent a = complex(-0.5, sqrt(3)/2);    % Rotation vector
  %
  % Build conversion matrix_type (constant)
  persistent A = [1, 1, 1;
  1, a, a^2;
  1, a^2, a]./3;
  %
  % Make abc a column vector
  if (length(abc)>3)
    sc = [];
    err = "Too many components!";
  elseif (length(abc)<3)
    sc = [];
    err = "Too few components!";
  else
    abc = reshape(abc,[3,1]);
    sc = A*abc;
    err = "";
  endif
endfunction
________________________________________

Here a is a constant and A is a constant matrix.  I used persistent in hopes
it would prevent them from being redefined every time the function is called.
In C++ I would use the "const" keyword.

Regards,
Allen
--
Allen Windhorn P.E. (Mn), CEng| Senior Principal Engineer
Leroy-Somer Americas | Kato Engineering, Inc.
2075 Howard Dr. West | North Mankato, MN 56003 | USA
T +1 507-345-2782 | F +1 507-345-2798
[hidden email] | [hidden email]



Reply | Threaded
Open this post in threaded view
|

Re: Constants in functions?

Octave - General mailing list

On 05/03/2020 20:12, Windhorn, Allen E [ACIM/LSA/MKT] wrote:

> What's the accepted way to define a constant inside a function?  Example:
> ____________________________________
> function [sc, err] = symmcomp(abc)
>    % Function accepts [abc] complex components and delivers symmetrical
>    % components as a vector [x0, x1, x2] of complex values
>    persistent a = complex(-0.5, sqrt(3)/2);    % Rotation vector
>    %
>    % Build conversion matrix_type (constant)
>    persistent A = [1, 1, 1;
>     1, a, a^2;
>     1, a^2, a]./3;
>    %
>    % Make abc a column vector
>    if (length(abc)>3)
>      sc = [];
>      err = "Too many components!";
>    elseif (length(abc)<3)
>      sc = [];
>      err = "Too few components!";
>    else
>      abc = reshape(abc,[3,1]);
>      sc = A*abc;
>      err = "";
>    endif
> endfunction
> ________________________________________
>
> Here a is a constant and A is a constant matrix.  I used persistent in hopes
> it would prevent them from being redefined every time the function is called.
> In C++ I would use the "const" keyword.
>
> Regards,
> Allen


An ugly (because IIRC Octave doesn't have nested functions) way:

function retval = TWO()

   retval = 2;

endfunction

.

The above function will return 2. You can similarly return matrix, row o
column vector, etc.


--Sergei.



Reply | Threaded
Open this post in threaded view
|

Re: Constants in functions?

siko1056
On 3/6/20 3:36 AM, Sergei Steshenko via Help list for GNU Octave wrote:

>
> On 05/03/2020 20:12, Windhorn, Allen E [ACIM/LSA/MKT] wrote:
>> What's the accepted way to define a constant inside a function?  Example:
>> ____________________________________
>> function [sc, err] = symmcomp(abc)
>>    % Function accepts [abc] complex components and delivers symmetrical
>>    % components as a vector [x0, x1, x2] of complex values
>>    persistent a = complex(-0.5, sqrt(3)/2);    % Rotation vector
>>    %
>>    % Build conversion matrix_type (constant)
>>    persistent A = [1, 1, 1;
>>            1, a, a^2;
>>            1, a^2, a]./3;
>>    %
>>    % Make abc a column vector
>>    if (length(abc)>3)
>>      sc = [];
>>      err = "Too many components!";
>>    elseif (length(abc)<3)
>>      sc = [];
>>      err = "Too few components!";
>>    else
>>      abc = reshape(abc,[3,1]);
>>      sc = A*abc;
>>      err = "";
>>    endif
>> endfunction
>> ________________________________________
>>
>> Here a is a constant and A is a constant matrix.  I used persistent in
>> hopes
>> it would prevent them from being redefined every time the function is
>> called.
>> In C++ I would use the "const" keyword.
>>
>> Regards,
>> Allen
>
>
> An ugly (because IIRC Octave doesn't have nested functions) way:
>
> function retval = TWO()
>
>   retval = 2;
>
> endfunction
>
> .
>
> The above function will return 2. You can similarly return matrix, row o
> column vector, etc.
>
>
> --Sergei.
>

Dear Allen,

Your code seems fine to me.  Using "persistent" [1] avoids the variables
to be redefined each time the function is invoked.  And if you do not
change it's value, it is "constant".  So what it is exactly you are
worried about?

Do you fear the value to be changed accidentally (by whom)?  How should
this happen, when you write the function?  In that case you can create a
subfunction [2], as suggested by Sergei, plus defining "retval" as
"persistent" for performance reasons (the variable "retval" would be
recreated in each function call).  I do not see a benefit of
nested-functions [3] in this scenario.

Neither Octave nor Matlab have a concept of C++ "const" variables.  Only
within classdef classes you can define constant properties [4].  But the
overhead may not be justified.

HTH,
Kai

[1] https://octave.org/doc/v5.2.0/Persistent-Variables.html
[2] https://octave.org/doc/v5.2.0/Subfunctions.html
[3] https://octave.org/doc/v5.2.0/Nested-Functions.html
[4] https://octave.org/doc/v5.2.0/Properties.html


Reply | Threaded
Open this post in threaded view
|

Re: Constants in functions?

siko1056
On 3/7/20 7:13 AM, Nicklas Karlsson wrote:

>> On 3/6/20 3:36 AM, Sergei Steshenko via Help list for GNU Octave wrote:
>>>
>>> On 05/03/2020 20:12, Windhorn, Allen E [ACIM/LSA/MKT] wrote:
>>>> What's the accepted way to define a constant inside a function?  Example:
>>>> ____________________________________
>>>> function [sc, err] = symmcomp(abc)
>>>>    % Function accepts [abc] complex components and delivers symmetrical
>>>>    % components as a vector [x0, x1, x2] of complex values
>>>>    persistent a = complex(-0.5, sqrt(3)/2);    % Rotation vector
>>>>    %
>>>>    % Build conversion matrix_type (constant)
>>>>    persistent A = [1, 1, 1;
>>>>            1, a, a^2;
>>>>            1, a^2, a]./3;
>>>>    %
>>>>    % Make abc a column vector
>>>>    if (length(abc)>3)
>>>>      sc = [];
>>>>      err = "Too many components!";
>>>>    elseif (length(abc)<3)
>>>>      sc = [];
>>>>      err = "Too few components!";
>>>>    else
>>>>      abc = reshape(abc,[3,1]);
>>>>      sc = A*abc;
>>>>      err = "";
>>>>    endif
>>>> endfunction
>>>> ________________________________________
>>>>
>>>> Here a is a constant and A is a constant matrix.  I used persistent in
>>>> hopes
>>>> it would prevent them from being redefined every time the function is
>>>> called.
>>>> In C++ I would use the "const" keyword.
>>>>
>>>> Regards,
>>>> Allen
>>>
>>>
>>> An ugly (because IIRC Octave doesn't have nested functions) way:
>>>
>>> function retval = TWO()
>>>
>>>   retval = 2;
>>>
>>> endfunction
>>>
>>> .
>>>
>>> The above function will return 2. You can similarly return matrix, row o
>>> column vector, etc.
>>>
>>>
>>> --Sergei.
>>>
>>
>> Dear Allen,
>>
>> Your code seems fine to me.  Using "persistent" [1] avoids the variables
>> to be redefined each time the function is invoked.  And if you do not
>> change it's value, it is "constant".  So what it is exactly you are
>> worried about?
>
> If it's supposed to not change Octave will make complaint in case of a misstake. It is very common in other programming languages and if there is compiler it is usually able to figure this already before program is run.
>
>> ...
>> Neither Octave nor Matlab have a concept of C++ "const" variables.  Only
>> within classdef classes you can define constant properties [4].  But the
>> overhead may not be justified.
>
> Consider this a missing feature though it would break compability with Matlab. Then value is not supposed to change I usually always make it a constant and consider it a good programming practice.
>
>
> Nicklas Karlsson
>


Please keep the mailing list in the CC so others can benefit from our
conversation.

You can file a bug report about this.  But as it is incompatible with
Matlab and the interpreter currently does not benefit from a constant
declaration, I do not think many people are keen to work on this extension.

Workarounds to avoid mistakes are discussed in this thread.

HTH,
Kai


Reply | Threaded
Open this post in threaded view
|

RE: Constants in functions?

Windhorn, Allen E [ACIM/LSA/MKT]
> -----Original Message-----
> From: Help-octave On Behalf Of Nicklas Karlsson
>
> Your code seems fine to me.  Using "persistent" [1] avoids the variables
> to be redefined each time the function is invoked.  And if you do not
> change its value, it is "constant".  So what it is exactly you are
> worried about?

No worries, just wondering if there were a better way.  Thanks to all for
the advice.

Regards,
Allen