1

This question is definitely a stupid question. But, coming from C; I'm having trouble adding header files or "an initialization" file to my pari-gp code. This is to mean; I have a 1hr compile of code to make one vector; and I can use that vector once initialized; but I want to make a file of this vector such that I can access it once it's compiled once.

Here's the code without a header file; which takes about an hour to compile (given the series precision/numerical precision which are set to 100).

\p 100
\ps 100
Phi_Inv(w,l,{n=100}) =
{
    my(out = 0);
    for(i=0,n,
        out = w*exp(out)/(exp(l*(n+1-i))+w)
    );
    out;
}




beta_init(n) = {
    beta_taylor = vector(100,i,polcoef(Phi_Inv(w,l,n),i-1,w));
    print(beta_taylor);
}

Rather than the brutal assignment of beta_taylor; and the caveman like print(beta_taylor), how can I write this to an initialization file I can package with the script. That is; an X mb file with all the coefficients neatly packed together. And if the file is lost, just run the code (which will take an hour) to write the initialization file again.

I mean, how would I properly do #include test.h where test.h is just a very long list of Taylor series values. So that I can just include this file, and write beta_taylor[i] for the i'th function. Such that it's as simple as including variables, like in C. I know I'm missing something simple and it's frustrating--making me feel stupid.

I'm mostly just asking about the syntax to go about and do this. I think I know how; but I imagine it's not the best way.

Any help or suggestions are greatly (And I really mean it, Greatly) appreciated.

To make a long story short; how do I save beta_taylor as a file we load when we initialize the program, and if the file is deleted we can save the program again by running the code for an hour?

Regards

Richard Diagram
  • 209
  • 1
  • 7
  • 1
    The Phi_Inv(w,1,n) call should be moved out of the vector: it does not depend on i and makes your code 100 times slower than it should be. – K.B. Aug 24 '21 at 11:34
  • 1
    In fact, beta_taylor = Vec(Phi_Inv(w,1,n), 100); would be an even better solution. – K.B. Aug 24 '21 at 11:36
  • @K.B. Ha, yeah I noticed that right after I posted this. I was repeatedly calculating ```Phi_Inv```. I changed it to ```beta_taylor = Phi_Inv(w,l,n)``` and I just wrote ```beta_taylor``` to a file, and call it as a ```"t_SER"``` type. I avoided the vector stuff, I just handle it as a series instead. Thanks for the comment though! If I didn't notice it that would've helped a lot ^_^. – Richard Diagram Aug 25 '21 at 00:13

2 Answers2

1

So you want to serialize your vector of numbers to a file and read it back in later?

writebin() to the rescue. Something like

beta_init(n) = {
    beta_taylor = vector(100,i,polcoef(Phi_Inv(w,l,n),i-1,w));
    writebin("beta_taylor.dat", beta_taylor);
}

Run the function in one gp session, and then in another session, beta_taylor=read("beta_taylor.dat").


Compiling your code first with gp2c before running it to calculate the numbers will speed things up if you're not already doing that, btw. gp2c-run makes it easy by compiling a file and starting a new gp session with the resulting shared library already loaded. You might also look into if the parallel operations can be used here to speed up the initial computation; reading the documentation for parvector() I don't think it can be, though, because of that mysterious l variables used in beta_init() that I don't see you define anywhere, but you might be able to re-phrase your equation with hardcoded constants or something.

Shawn
  • 47,241
  • 3
  • 26
  • 60
  • THANK YOU SO MUCH! That's exactly what I wanted! I'm planning to compile this, not at that stage yet. The variable ```l``` is a complex number in the right half plane; I need it as a free variable at the moment. But when the time comes (later in the program); we just plug in ```l = log(2)``` (for instance). I'm fairly new to pari-gp; I've only ever really coded C--so this is really helpful! Thanks again! – Richard Diagram Jul 15 '21 at 03:11
  • quick question. If I just add a ```beta_taylor = read("beta_taylor.dat");``` in the beginning of my file--so long as the file is in the same folder as the source; would this work essentially like an ```#include beta_taylor.dat``` command? Such that I could compile these things as one? – Richard Diagram Jul 15 '21 at 04:36
  • @RichardDiagram The file would have to exist at runtime. It's not like the C preprocessor's include directive. – Shawn Jul 15 '21 at 22:19
  • Figured as much; thought I'd ask anyway. Thanks again! Super helpful; already have been able to run trials much faster! – Richard Diagram Jul 16 '21 at 03:35
  • I've taken the route to load a basic set of GP-routines from an `__init.gp`-file, which is called/included at Pari/GP-start. This can be configured in the GP-initialization file. The path for data- or such initializing files can also be set to be different to your workig file with your GP-sources. If you need this more precise I could show my init-routine. – Gottfried Helms Oct 17 '21 at 08:58
1

An initialization for Pari/GP, at program start: (file gprc.txt in directory of gp.exe)


lines  = 25
colors = "brightfg"
histfile = "gp_history.txt"
breakloop = 0
help = "@ perl\\perl gphelp.pl -detex -ch 10 -cb 11 -cu 12"
prompt = "gp >"
prompt_cont="gpc>"
datadir = "u://paritty/syntax/_v11/"
path = "u://paritty/syntax/_v11/"
primelimit = 100 000 000
parisizemax = 1 000 000 000
read  "__init.gp"
echo = 0

The file __init.gp contains my generally used functions; commands to read precomputed data-vectors can of course be included there. If no path is indicated it will be searched in the directory given in the path= statement.