Bill Allombert on Sun, 10 Sep 2023 15:08:19 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Manually transpile Python code to GP: what about class / __name__ ?


On Sun, Sep 10, 2023 at 10:40:18AM +0200, hermann@stamm-wilbrandt.de wrote:
> Perhaps a bit of motivation first.
> RSA_numbers_repo is available in Python, nodejs/JavaScript and PARI/GP.
> 
> On 2023-09-09 20:43, Bill Allombert wrote:
> > > What remains is transpilation of Python class RSA.
> > > Is there a "class" concept in GP?
> > 
> > No, GP is not object-oriented at all. It has some minimal
> > functional-programming features
> > (anonymous functions and closure).
> > 
> Thanks, what I need is that it "looks similar" to Python class code.
> In order to port easily code from one language to the other.
> 
> > > It seems so, as "." is not a normal name character:
> > 
> > You know, there is a GP manual you could read...
> > 
> I did of course, but searched for "class" and found many hits, but not on GP
> class.

Because there are no classes in GP in the object-oriented programmning/Python sense.

> Thanks, I found "2.8 Member functions":
> https://pari.math.u-bordeaux.fr/pub/pari/manuals/2.15.4/users.pdf#page=48
> 
> The preceding chapters on functions and closures helped as well.
> 
> I am happy with RSA member function "square_sums_4()".
> Function comment "Example" section shows how to use.
> In case mailing software scambles display, here is function on github:
> https://github.com/Hermann-SW/RSA_numbers_factored/blob/main/pari/RSA_numbers_factored.gp#L1525-L1552
> 
> RSA.square_sums_4=x->{
> }

The whole problem is that this does not allow to change RSA.

? RSA.set=x->RSA[1]=x
%1 = (RSA)->x->RSA[1]=x
? RSA=[0,0];
? RSA.set(2)
%3 = 2
? RSA
%4 = [0,0]

> I have few questions I was not able to resolve by reading GP manual and
> experiment:
> 
> 1)
> Is it possible to define a member function without parameter?

you can do this
RSA.factored_2=()->{
...
}

> 2)
> I was not able to get the syntax right for a parameter with default value.
> I used below workaround.
> Is is possible to define default value for GP member function parameter
> directly?
> 
> factored(mod4=-1)=
> {
> \\  ...
>     return([r[1..4]|r<-rsa,has_factors(r,mod4)]);
> }
> RSA.factored=factored;

RSA.factored = (mod4=-1)->{
}

> 
> 
> 3)
> I did not need sofar, but is it possible to pass multiple parameter"s to GP
> member function?
> (this likely does not make sense for what GP member functions have been
> implemented)

You can do
RSA.fun=(x,y)->{...
}
If this is what you have in mind.

Cheers,
Bill