Post Reply 
logging a function's argument
09-25-2022, 07:40 PM
Post: #1
logging a function's argument
With the 50g, it was possible to have a function with the side effect of logging the argument by appending it to a list. Say something like this.

Code:

XARG
<< ->  X
  << 'XLST' X STO+ X >> 
>>

where 'XLST' starts out as an empty list.

My motivation for such a function was to explore certain algorithms such as numeric solving and numeric integration. Afterwards, I could look at the 'XLST' to see what the algorithm did. It worked like a charm.

My attempts for doing the same with the Prime have been less successful. I tried

Code:
EXPORT XARG(x)
BEGIN
 L1:=CONCAT(L1,x);
 RETURN x;
END;

and
Code:
#cas
xarg(x):=
BEGIN
 L1:=CONCAT(L1,x);
 RETURN x;
END;
#end

These work fine when executed directly from the command line. But when used within fsolve() or int(), neither works as hoped.

Any ideas on how to make this work?
Find all posts by this user
Quote this message in a reply
09-26-2022, 12:39 PM
Post: #2
RE: logging a function's argument
Did you try L1(0):=x ?
Find all posts by this user
Quote this message in a reply
09-26-2022, 02:55 PM
Post: #3
RE: logging a function's argument
(09-26-2022 12:39 PM)roadrunner Wrote:  Did you try L1(0):=x ?

Sorry, I guess I didn't explain very well. I want to keep a record of the values at which a function was evaluated multiple times. So I'm looking for a function, xarg(x), which returns x but also appends x to a list.

So say I wanted to numerically solve the equation e^x - 5x = 0 and I wanted to watch the values of x at which the function was evaluated in the solving process. I could replace one of the x's with xarg(x) within the solve command.

Code:
fsolve((e^x-5*xarg(x)) = 0.,x,2.)  ->  2.54264135777 (correct)

but the list only contains {x}. When I do this on the 50g, the list contains:

Code:
{ 2. 2.000001 2.000051 2.002551 2.127551 2.90361431698 2.51558265849 2.54439280754 2.54263530927 2.5426413579 2.54264135777 }

Likewise, for numeric integrals,

Code:
∫(e^x-5*xargcas(x),x,-1.,1.)  ->  2.35040238729 (correct)

but again the list just contains a variable. On the 50g the list has all 127 values at which the function was evaluated.

Code:
{ 0. -.6875 .6875 -.3671875 .3671875 -.9140625 .9140625 -.1865234375 .1865234375 -.5361328125 .5361328125 ...  }


It appears that the function is evaluated symbolically first before being plugged into fsolve() or ∫() which reverts e^x-5*xargcas(x) back to e^x-5*x

Any way to produce the desired list?
Find all posts by this user
Quote this message in a reply
09-26-2022, 03:12 PM
Post: #4
RE: logging a function's argument
I have previously included an assignment within a return statement.
Something like
return lst:={1,2,3}
Which both assigned the list to lst and returned the list.

Which makes me wonder whether something like
Fun(lst:={1,2,3})
Might help if you can call fun with a list.

Or perhaps
Fun(lst(0):=1,lst(0):=2,lst(0):=3)
Instead of fun(1,2,3)?

(the lst(0) syntax keeps appending to lst)

Stephen Lewkowicz (G1CMZ)
https://my.numworks.com/python/steveg1cmz
Visit this user's website Find all posts by this user
Quote this message in a reply
09-26-2022, 05:14 PM
Post: #5
RE: logging a function's argument
(09-26-2022 03:12 PM)StephenG1CMZ Wrote:  Which makes me wonder whether something like
Fun(lst:={1,2,3})
Might help if you can call fun with a list.

But to use the function with solve or integration, the function has to have a single number argument and return a number.


(09-26-2022 03:12 PM)StephenG1CMZ Wrote:  (the lst(0) syntax keeps appending to lst)

When I try lst(0):=5; it replaces the last element with 5 rather than appending 5.
Find all posts by this user
Quote this message in a reply
09-27-2022, 09:21 AM
Post: #6
RE: logging a function's argument
That's because the commands you are calling are doing some preprocessing, in order to avoid evaluating parameters again and again.
If you quote the function as argument of e.g. romberg or gaussquad, you will see the values.
Code:

argx(a):=begin l.append(a); return a; end;
l:=[];
romberg('sin(cos(argx(x)))',x,-1,1);
Find all posts by this user
Quote this message in a reply
09-28-2022, 04:44 PM
Post: #7
RE: logging a function's argument
(09-27-2022 09:21 AM)parisse Wrote:  If you quote the function as argument of e.g. romberg or gaussquad, you will see the values.
Code:
argx(a):=begin l.append(a); return a; end;
l:=[];
romberg('sin(cos(argx(x)))',x,-1,1);

Thanks, that's just what I was looking for.

It works with romberg() and int() with finite limits, but not int() with infinite limits. Is there a way to call gaussquad() directly on the Prime like you can in XCAS?

I still couldn't get fsolve() to work, but I am pleased to get integration working.
Find all posts by this user
Quote this message in a reply
09-29-2022, 05:44 PM
Post: #8
RE: logging a function's argument
Unfortunately it's not possible to call gaussquad in the public firmware version of the Prime.
You can run a full calculator implementation of Giac on the Casio FXCG50, unlocked Numworks N0110 or ndless-compatible TI Nspire CX/CX2. I would love to see something similar on the Prime!
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)