Am 02.02.2022 um 16:42 schrieb Ekkehart Schlicht:
I see that I have to declare function scalar inner(matrix *a) rather
than inner(matrix a) in the pointer case. So only the first question
remains.
Sorry for that.
No problem, you found the right answer yourself.
On 02.02.2022 16:24, Ekkehart Schlicht wrote:
> Hi,
>
> I am new to Gretl and have two perhaps silly question concerning the
> use of pointers. In the Hansl guide p.14 it is explained that
> parameters are passed to functions "by value" but can also be passed
> by pointers. As I am dealing with very large sparse matrices, I had
> the idea to pass them by pointer rather than value in order to save
> time (for not duplicating the matrix for use in a function in each of
> some thousand iterations) and use less memory space. Does this make
> sense?
Yes, it does.
BTW, there is another way to achieve this efficiency, but only in the
case where you don't want to change the pointer argument in the
function. In your example function, you just operated _with_ the matrix
input a, but didn't operate _on_ it or change it. Then you could declare
your non-pointer argument as "const", in which case gretl would avoid
the unnecessary data copy. Like this:
function scalar inner(const matrix a) # notice no *
return a * a'
end function
Speedwise the effect should be the same as using a pointer argument. But
the price to pay for the slightly simpler call syntax (no & sign needed,
and you can pass an anonymous on-the-fly matrix I think) is that you're
not allowed to change 'a' inside the function in any way.
cheers
sven