Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Messages - kuan

#1
newLISP in the real world / Re: curl bindings for newLISP
November 29, 2013, 10:23:05 PM
Good work. Thanks a lot.
#2
Thank you, lutz.

I transform "a" to float explicitly:

(set 'af (map float a))

then, the LAPACK_dgels works well.



"pack" looks like malloc or other allocate memory functions in C. It allocates continuous memory and assigns value of some type (float, int etc) to every position. This is so-called "packs expressions into a binary format". The return value of pack is string filled up with "00".



am I right?



PS:

> (file? "/usr/")
true
> (directory? "/usr/")
true

"file?" function "will also return true for directories". We've already had "directory?". This is why "directory?" function exits. When I want to test a path whether is a file, it can't give me an answer. Certainly, I can use "(not (directory? path))" , but it is not direct.
#3
I think "char" or "int" doesn't matter, when I change  "int" to/from "char", the result is the same as before.

And, when I do as you said, replace (address aptr) with aptr, strangely, the result doesn't change.

But when I test C array API with my own function below, the result is good. Here is code:


int sm(int m, int n, double * p)
{
    double *p_end;
    p_end = p + (m * n) - 1;
    for (;p <= p_end; p++)
        (*p) = (* p) + 1;
    return 0;
}


here is my test.lsp:
(set 'LIB "scalam.so")
(import LIB "sm" "int" "int" "int" "void*")
(set 'a '(1 1 1 2 3 4 3 5 2 4 2 5 5 4 3))
(setq m 5 n 3)
(set 'ap (pack (dup "lf" (* m n)) a))
(set 'info (sm m n ap))

output is right
> (unpack (dup "lf" 15) ap)
(2 2 2 3 4 5 4 6 3 5 3 6 6 5 4)


My all questions are:

1.in newLISP, list is contiguous in memory? just as array? or anything else?

2.To compute with address or not doesn't matter, I am confused.

3.When I test my own sm function with b which has negative number, there are error with the negative elements:
>(set 'bp (pack (dup "lf" 10) b)
>(set 'info (sm 5 2 (address bp)))
> (unpack (dup "lf" 10) bp)
(1.844674407e+19 1.844674407e+19 13 15 15 13 17 17 19 17)

so, the reason is the negative number? and the more negative numbers, the more error
> (set 'b '(-10 -3 -12 -14 -14 12 16 16 18 16))
(-10 -3 -12 -14 -14 12 16 16 18 16)
> (set 'bp (pack (dup "lf" 10) b))
"000000000000�C000000000000�C000000000000�C000000000000�C000000000000�C000000000000(@0000000000000@0000000000000@0000000000002@0000000000000@"
> (set 'info (sm 5 2 (address bp)))
0
> (unpack (dup "lf" 10) bp)
(1.844674407e+19 1.844674407e+19 1.844674407e+19 1.844674407e+19 1.844674407e+19
13 17 17 19 17)

am I clear? Sorry for my terrible English.



PS: the dgels functioin in lapacke.h
lapack_int LAPACKE_dgels( int matrix_order, char trans, lapack_int m,
                          lapack_int n, lapack_int nrhs, double* a,
                          lapack_int lda, double* b, lapack_int ldb );


#ifndef lapack_int
#if defined(LAPACK_ILP64)
#define lapack_int              long
#else
#define lapack_int              int
#endif
#endif

#define LAPACK_ROW_MAJOR               101
#define LAPACK_COL_MAJOR               102


in dgels fortran source code, I found:
*> DGELS solves overdetermined or underdetermined real linear systems
*> involving an M-by-N matrix A, or its transpose, using a QR or LQ
*> factorization of A.  It is assumed that A has full rank.
*>
*> The following options are provided:
*>
*> 1. If TRANS = 'N' and m >= n:  find the least squares solution of
*>    an overdetermined system, i.e., solve the least squares problem
*>                 minimize || B - A*X ||.
*>
*> 2. If TRANS = 'N' and m < n:  find the minimum norm solution of
*>    an underdetermined system A * X = B.
*>
*> 3. If TRANS = 'T' and m >= n:  find the minimum norm solution of
*>    an undetermined system A**T * X = B.
*>
*> 4. If TRANS = 'T' and m < n:  find the least squares solution of
*>    an overdetermined system, i.e., solve the least squares problem
*>                 minimize || B - A**T * X ||.
*>
#4
I want to compute a matrix with lapacke.

The example is here http://www.netlib.org/lapack/lapacke.html#_calling_tt_dgels_tt">http://www.netlib.org/lapack/lapacke.ht ... t_dgels_tt">http://www.netlib.org/lapack/lapacke.html#_calling_tt_dgels_tt



here is my code:
(set 'LAPACKE "/usr/local/lib/liblapacke.so")
(import LAPACKE "LAPACKE_dgels" "int" "int" "char" "int" "int" "int" "void*" "int" "void*" "int")
(set 'a '(1 1 1 2 3 4 3 5 2 4 2 5 5 4 3))
(set 'b '(-10 -3 12 14 14 12 16 16 18 16))

(setq m 5 n 3 nrhs 2 lda 3 ldb 2)

(set 'aptr (pack (dup "lf" (* m n)) a))

(set 'bptr (pack (dup "lf" (* m nrhs)) b))

(set 'info (LAPACKE_dgels 101 78 m n nrhs (address aptr) lda (address bptr) ldb))


but the rsult is wrong.
(unpack (dup "lf" (* m nrhs)) bptr)
-> (1380.761318 1380.761318 1.844674407e+17 1.844674407e+17 1.844674407e+17 1.844674407e+17
 -1.415684734e+19 -1.415684734e+19 -1.153518066e+19 -1.153518066e+19)


I don't know where is wrong, can anyone give me some advice?

Thank you.

kuan.
#5
thank you lutz, it does work.
#6
newLISP in the real world / about return value from gsl
December 08, 2012, 09:47:43 AM
I want to do some scientific computing with gsl. but encounter some error. below is my code

> (import LIB "gsl_complex_rect" "void*" "double" "double")
gsl_complex_rect@7FBAD0403FD0
> (unpack "lf lf" (gsl_complex_rect 2.0 3.0))
Segmentation fault (core dumped)

the complex defined in gsl like this:

typedef struct
     {
       double dat[2];
     } gsl_complex;

and the funciton gsl_complex_rect like this:
Quote


 -- Function: gsl_complex gsl_complex_rect (double X, double Y)

     This function uses the rectangular Cartesian components (X,Y) to

     return the complex number z = x + i y.  An inline version of this

     function is used when `HAVE_INLINE' is defined.


how can I print the complex returned by gsl_complex_rect?

thanks .