This text explains, how one can compile Pari to be used from Excel.
The setup and needed steps are almost identical as one could do for
Maple - so please read the docu for that first. Here mainly I want
to care for special issues concerning Excel. Using the pari_xl.c
as base for the project, adding pari_xl.def (to export functions)
and using __cdecl with "Multithreaded DLL" (in Project Settings,
C/C++ tab, category Codeg Generation) you should have no problems
to compile into the DLL (hence I have not uploaded the complete
MSVC project again).
Communication between Excel and Pari is done through strings.
Well, Excel has no symbolics, so one is very limited in using Pari,
but it could do for numerical requests. Since Excel does not have
reasonable complex arithmetics (it is extremely slow and purely by
strings) it reduces to real floating points (or write your own VBA
library for complex arithmetics using 2 dim structures).
Then I was too lazy to work out a "worksheet interface" and just
did the stuff for VBA. One reason is: together with Windows there
are many options how Excel may be configured and especially it is
language dependent - which partially even is true for programming
with VBA (note that 1000*Pi is not like 3,141.59 on all PCs, if you
are non-english - it may be 3141,59 on my German PC).
At least I would like to care for this decimal separator ... But
a Pari command may conain commata ... So be careful converting
between numbers and strings, which Pari should understand. For the
worksheets one would have to care for at least to ways to enter
strings: as usual with quotation marks or through cell formatting
(which is far more convenient).
Ok, so VBA only. So open the VBA project and first set the correct
path to the DLL (do it in the module m_declareDLL), so Excel can
find pari_xl.dll (the loaction does not matter).
As explained in the docu on using Pari with Maple it is essential,
that Pari is closed properly. For this an automatic procedure was
enclosed, which does that (if you do not allow auto macros in your
Excel for security reasons care for that by yourself).
The VBA function pari takes a string as input and returns Pari's
result. The input is normed to the decimal separator and then this
string is converted to a 'byte array' (which is processed in C in
the DLL; one just has to know, how an Excel string is seen bytewise
in C - but do not ask me, how it is for a Chinese setting), hands
it to the DLL and gets its result in another byte array, which is
provided as space from Excel and understood as string.
There are 2 reasons for that: first it is a way to share memory
between Excel and an external program like the DLL (without using
the MS SDK stuff, so it works for all compilers, not only MSVC).
The other point is: the Pari sources seem to compile only for the
calling convention __cdecl, while MS expects __stdcall and mixing
seems to work for byte arrays.
The other VBA function are value_pari and evalf_pari, they should
be used if one wants floating point results.
Actual work is done in the C program. Excel string <---> C string
is by interlacing 0 between each character, so one can handover
commands to Pari and receive its answers (in case of errors Pari's
error messages are sent back). This should work for Visual Basic
as well - but I would not trust that MS keeps its string definitions
for ever (say in .NET or newer versions of Excel).
The second module in the Excel project just gives some examples,
how one can call Pari and what happens on return values. In good
cases that could be used as numerical value (for example it can
replace non-precise functions in Excel, but as already said the
complex case is missing).
AVt, Oct 2006