On Mon, Apr 12, 2021 at 06:03:34PM -0700, Tobias Neumann wrote:
> I've noticed that FriCAS doesn't have a numerical implementation of 
> polylog's yet.

I wonder what do you need.  I have partial multiple precision
implementation, which does not cover full argument range but
seem to work well for some arguments.

Concerning calling to C/Fortran, maybe the following will
help.  First, modification of your C file:

---------------<test.c cut here>-----------------

double
testfun1(double arg) {
    return 3.14 * arg;
}

void
testfun2(double arg, double* realpart, double* imagpart) {
    *realpart = 1.0 * arg;
    *imagpart = 3.14 * arg;
}

---------------<cut here>-----------------

Next, Lisp wrapper:

---------------<tst.lisp cut here>-----------------

(fricas-lisp::fricas-foreign-call |testfun1| "testfun1" fricas-lisp::double
                (arg fricas-lisp::double))

(defun |testfun2| (arg)
    (let* (
          (tmp-bufr (sb-alien:make-alien sb-alien:double 1))
          (tmp-bufi (sb-alien:make-alien sb-alien:double 1)))
         (sb-alien:alien-funcall
              (sb-alien:extern-alien "testfun2"
                   (sb-alien:function sb-alien:void
                           SB-ALIEN::double (* t) (* t)))
                        arg tmp-bufr tmp-bufi)
         (cons (sb-alien:deref tmp-bufr 0)
               (sb-alien:deref tmp-bufi 0))

     )
)

---------------<cut here>-----------------

C compilation:

gcc -Wall -O -shared -o libtest.so test.c

In FriCAS:

(1) -> )lisp (sb-alien::load-shared-object "./libtest.so")  
Value = #P"./libtest.so"
(1) -> )lisp (load (compile-file "tst.lisp"))

; compiling file "/mnt/lv3/fricas/axp19/pp1/pp19/tst.lisp" (written 16 APR 2021 
01:47:09 PM):

; wrote /mnt/lv3/fricas/axp19/pp1/pp19/tst.fasl
; compilation finished in 0:00:00.012
Value = T
(1) -> valr := testfun1(10.0::DoubleFloat)$Lisp pretend DoubleFloat

   (1)  31.400000000000002
                                                            Type: DoubleFloat
(2) -> valc := testfun2(10.0::DoubleFloat)$Lisp pretend Complex(DoubleFloat)

   (2)  10.0 + 31.400000000000002 %i
                                                   Type: Complex(DoubleFloat)

Comments:
- I am not sure what matters there, maybe fact that I compiled the code,
  maybe direct use of low-level sbcl constructs
- when arguments are passed by value 'fricas-foreign-call' allows
  relatively easy definition.  ATM 'fricas-foreign-call' seem to
  miss support for C 'long' but that could be easily added.  Also,
  needed symbols are not exported.  For general use we probably
  should rename it to 'fricas_foreign_call' and make names
  lowercase
- pointer arguments are tricky.  When doing call we may need to
  allocate memory, but for performance we prefer passing addresses
  of existing FriCAS data.  We may need extra copies, for Complex
  we need to cons wrapper node
- extra complication are caused by C structs

-- 
                              Waldek Hebisch

-- 
You received this message because you are subscribed to the Google Groups 
"FriCAS - computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/fricas-devel/20210416151657.GB36052%40math.uni.wroc.pl.

Reply via email to