proc `*=`*[N: static[int]](v: var Vector64[N], k: float64) {. inline .} = scal(N, k, v.fp, 1)
Here scal is imported from a C header:
proc scal(N: int, ALPHA: float64, X: ptr float64, INCX: int)
{. header: header, importc: "cblas_dscal" .}
Now, it seems that the Nim compiler sometimes sees N as a constant and decides to call scal passing just three arguments, so that the compilation fails at the GCC step.
The simplest example that I have been able involves sequences of vectors:
import linalg
proc sum[N: static[int]](vs: seq[Vector64[N]]): Vector64[N] =
result = zeros(N)
for v in vs:
result += v
echo sum(@[
randomVector(5),
randomVector(5),
randomVector(5)
])
This fails with
/home/papillon/esperimenti/example/nimcache/example_example.c: In function ‘HEX2BHEX3D_112413’:
/home/papillon/esperimenti/example/nimcache/example_example.c:205:2: error: incompatible type for argument 2 of ‘cblas_daxpy’
cblas_daxpy(1.0000000000000000e+00, ((NF*) (w)), ((NI) 1), ((NF*) ((*v))), ((NI) 1));
^
In file included from /home/papillon/.bin/intel/composer_xe_2015.0.090/mkl/include/mkl.h:48:0,
from /home/papillon/esperimenti/example/nimcache/example_example.c:12:
/home/papillon/.bin/intel/composer_xe_2015.0.090/mkl/include/mkl_cblas.h:160:6: note: expected ‘double’ but argument is of type ‘NF *’
void cblas_daxpy(const MKL_INT N, const double alpha, const double *X,
^
/home/papillon/esperimenti/example/nimcache/example_example.c:205:2: error: too few arguments to function ‘cblas_daxpy’
cblas_daxpy(1.0000000000000000e+00, ((NF*) (w)), ((NI) 1), ((NF*) ((*v))), ((NI) 1));
Any idea how to make this work?
Because I want to keep track of vector and matrices dimension at compile time, so that the compiler will refuse to operations where the dimensions do not match.
In fact the compiler does a pretty good job at infering the right dimensions, see the examples in the README and and in the tests. I was hoping to write something that would feel easy to use like numpy, but still avoid common mistakes thanks to the static[int] guarantee
Thank you, I reported the bug here
I was able to produce an example where the linalg library is not involved.