www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - matrix business in D

reply "Yura" <min_yura mail.ru> writes:
Dear D programmers,

I am very new to D programming language. I just started to learn 
it as an alternative to python since the latter sometimes is too 
slow. My question is whether there some simple ways to solve 
linear algebra problems in D programming language? E.g. matrix 
multiplication, diagonalization, SVD decomposition? If there is 
something, I would definitely stick to D programming language in 
my projects.

PS I am not a proffesinal programmer and I am sorry if this 
question has already been discussed.

Thaks in advance!
Oct 17 2013
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 10/17/2013 01:31 PM, Yura wrote:
 Dear D programmers,

 I am very new to D programming language. I just started to learn it as
 an alternative to python since the latter sometimes is too slow. My
 question is whether there some simple ways to solve linear algebra
 problems in D programming language? E.g. matrix multiplication,
 diagonalization, SVD decomposition? If there is something, I would
 definitely stick to D programming language in my projects.

 PS I am not a proffesinal programmer and I am sorry if this question has
 already been discussed.

 Thaks in advance!
I don't know the answer but the following two threads seem relevant: http://forum.dlang.org/thread/aszatcbdiucurlnqrpjk forum.dlang.org http://forum.dlang.org/thread/rhldihvykaaneubyrjhj forum.dlang.org Ali
Oct 17 2013
prev sibling next sibling parent reply "Daniel Davidson" <nospam spam.com> writes:
On Thursday, 17 October 2013 at 20:31:38 UTC, Yura wrote:
 Dear D programmers,

 I am very new to D programming language. I just started to 
 learn it as an alternative to python since the latter sometimes 
 is too slow. My question is whether there some simple ways to 
 solve linear algebra problems in D programming language? E.g. 
 matrix multiplication, diagonalization, SVD decomposition? If 
 there is something, I would definitely stick to D programming 
 language in my projects.

 PS I am not a proffesinal programmer and I am sorry if this 
 question has already been discussed.

 Thaks in advance!
Please follow through with finding a D solution. But if you have not seen it yet, have a look at http://julialang.org/ as well. It may also fit your needs with more focus on mathematical programming.
Oct 18 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Daniel Davidson:

 Please follow through with finding a D solution. But if you 
 have not seen it yet, have a look at http://julialang.org/ as 
 well. It may also fit your needs with more focus on 
 mathematical programming.
Julia is a very new language, quite newer than D. I don't think it's a good idea to recommend it for real work. Bye, bearophile
Oct 18 2013
parent "Brian Rogoff" <brogoff gmail.com> writes:
On Friday, 18 October 2013 at 13:04:51 UTC, bearophile wrote:
 Julia is a very new language, quite newer than D. I don't think
 it's a good idea to recommend it for real work.
I don't think that the simple rule comparing age of the languages in question for risk assessment is very useful. Given all of the other variables, I'd be more likely to recommend Julia for numerical linear algebra today than D for the same role. That's not a slam on D, which I mostly like better than it's competition (C++, Rust, C, ...) but rather an observation that the Julia community is entirely focused on this domain. A really risk averse programmer who wouldn't consider Julia in this domain wouldn't consider D either -- Brian
Oct 20 2013
prev sibling next sibling parent "Geancarlo Rocha" <nope mailinator.com> writes:
Why not stick with scipy+numpy in python? Writing numerical code 
is painfully time consuming. It's also unlikely that your code 
will be more performant than those libraries', it takes a lot of 
expertise.


On Thursday, 17 October 2013 at 20:31:38 UTC, Yura wrote:
 Dear D programmers,

 I am very new to D programming language. I just started to 
 learn it as an alternative to python since the latter sometimes 
 is too slow. My question is whether there some simple ways to 
 solve linear algebra problems in D programming language? E.g. 
 matrix multiplication, diagonalization, SVD decomposition? If 
 there is something, I would definitely stick to D programming 
 language in my projects.

 PS I am not a proffesinal programmer and I am sorry if this 
 question has already been discussed.

 Thaks in advance!
Oct 18 2013
prev sibling next sibling parent "bachmeier" <nospam nospam.com> writes:
On Thursday, 17 October 2013 at 20:31:38 UTC, Yura wrote:
 Dear D programmers,

 I am very new to D programming language. I just started to 
 learn it as an alternative to python since the latter sometimes 
 is too slow. My question is whether there some simple ways to 
 solve linear algebra problems in D programming language? E.g. 
 matrix multiplication, diagonalization, SVD decomposition? If 
 there is something, I would definitely stick to D programming 
 language in my projects.

 PS I am not a proffesinal programmer and I am sorry if this 
 question has already been discussed.

 Thaks in advance!
I have done some linear algebra in D. If you are comfortable calling C functions, you can easily call into existing solutions, because it is trivial to call into C from D. I use Gretl http://gretl.sourceforge.net/ because it offers a convenient interface to commonly used BLAS and LAPACK functionality. GSL is another good choice https://www.gnu.org/software/gsl/. This has worked well for me because I am using D as a drop-in replacement for C. If you do not know C or otherwise want a D solution, there is SciD https://github.com/kyllingstad/scid/wiki. I've never had a reason to use it so I do not know how well it works.
Oct 20 2013
prev sibling parent reply "Yura" <min_yura mail.ru> writes:
Dear all,

Thank you for your replies!

Regarding Julia - it seems to be interesting, but - it is too 
fresh, and from what I understood, it is not compiled. I think D 
language would be more interesting for me and suitable for my 
needs (scientific computing).

Yes, numpy/scipy is OK, but since I have now some time I would 
like to learn one compiled language which is more close to the 
hardware,

"I have done some linear algebra in D. If you are comfortable
calling C functions, you can easily call into existing solutions,
because it is trivial to call into C from D."

This is very interesting since as you know lots of code is 
written in c. GSL is a good example. The only problem is how to 
use it. The thing is that i don't know c, but the question is 
whether I really need to be skilled in c to be able to call c 
functions. My gut feeling is that no, I don't need to be skilled. 
I have installed gsl on my computer. But what I need is a good 
example of a code/codes on how to call this library from d 
programming language. E.g. I have tried to use gsl. I have 
written a code in c (simple.c):

-------
#include <stdio.h>
#include <gsl/gsl_sf_bessel.h>

double fun(double x)
//main (void)
{
//  x = 5.0;
   double y = gsl_sf_bessel_J0 (x);
//  printf ("J0(%g) = %.18e\n", x, y);
   return y;
}
----------------
Also, I have written a di file (simple.di):
----------------
extern (C):
double fun(double);
----------------
And finally, d code (simple.d):
------------------
import std.stdio, std.string, std.array;
import std.conv;

import std.stdio;
import simple;

void main(){
     writeln( fun(10.0) );
}
------------------------------------

Unfortunately, when I compile it it says:

dmd simple.d simple.o
simple.d(8): Error: undefined identifier fun

Could one provide a working clear example how to use gsl in D?

I have tried SciD and it apparently works, though I did not test 
it so far. I think a tutorial on how to use D in scientific 
programming would be very appreciated and could attract more 
people to D.

PS Thank all of you for helping.












I use Gretl
http://gretl.sourceforge.net/ because it offers a convenient
interface to commonly used BLAS and LAPACK functionality. GSL is
another good choice https://www.gnu.org/software/gsl/. This has
worked well for me because I am using D as a drop-in replacement
for C."





On Thursday, 17 October 2013 at 20:31:38 UTC, Yura wrote:
 Dear D programmers,

 I am very new to D programming language. I just started to 
 learn it as an alternative to python since the latter sometimes 
 is too slow. My question is whether there some simple ways to 
 solve linear algebra problems in D programming language? E.g. 
 matrix multiplication, diagonalization, SVD decomposition? If 
 there is something, I would definitely stick to D programming 
 language in my projects.

 PS I am not a proffesinal programmer and I am sorry if this 
 question has already been discussed.

 Thaks in advance!
Oct 23 2013
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Wednesday, 23 October 2013 at 14:00:46 UTC, Yura wrote:
 Dear all,

 Thank you for your replies!

 Regarding Julia - it seems to be interesting, but - it is too 
 fresh, and from what I understood, it is not compiled. I think 
 D language would be more interesting for me and suitable for my 
 needs (scientific computing).

 Yes, numpy/scipy is OK, but since I have now some time I would 
 like to learn one compiled language which is more close to the 
 hardware,

 "I have done some linear algebra in D. If you are comfortable
 calling C functions, you can easily call into existing 
 solutions,
 because it is trivial to call into C from D."

 This is very interesting since as you know lots of code is 
 written in c. GSL is a good example. The only problem is how to 
 use it. The thing is that i don't know c, but the question is 
 whether I really need to be skilled in c to be able to call c 
 functions. My gut feeling is that no, I don't need to be 
 skilled. I have installed gsl on my computer. But what I need 
 is a good example of a code/codes on how to call this library 
 from d programming language. E.g. I have tried to use gsl. I 
 have written a code in c (simple.c):

 -------
 #include <stdio.h>
 #include <gsl/gsl_sf_bessel.h>

 double fun(double x)
 //main (void)
 {
 //  x = 5.0;
   double y = gsl_sf_bessel_J0 (x);
 //  printf ("J0(%g) = %.18e\n", x, y);
   return y;
 }
 ----------------
 Also, I have written a di file (simple.di):
 ----------------
 extern (C):
 double fun(double);
 ----------------
 And finally, d code (simple.d):
 ------------------
 import std.stdio, std.string, std.array;
 import std.conv;

 import std.stdio;
 import simple;

 void main(){
     writeln( fun(10.0) );
 }
 ------------------------------------

 Unfortunately, when I compile it it says:

 dmd simple.d simple.o
 simple.d(8): Error: undefined identifier fun

 Could one provide a working clear example how to use gsl in D?

 I have tried SciD and it apparently works, though I did not 
 test it so far. I think a tutorial on how to use D in 
 scientific programming would be very appreciated and could 
 attract more people to D.

 PS Thank all of you for helping.
Don't call everything the same name. At the very least don't have the di and d file with the same name. Once you've done that, it will compile but the linker will start to complain. You will need to link to the gsl and gslcblas libraries, making your compilation command this: dmd test.d simple.o -L-lgsl -L-lgslcblas The simplest possible example of using gsl would be this: simpleGSL.d import std.stdio; extern(C) double gsl_sf_bessel_J0(double); void main() { writeln(gsl_sf__bessel_J0(10)); } compile with dmd -L-lgsl -L-lgslcblas simpleGSL.d If you were doing this seriously you would want to create a load of d or di files containing the extern(C) declarations for all the different gsl things you need. Also, you might want to take a look at dstep: https://github.com/jacob-carlborg/dstep which might be able to auto-generate them all for you.
Oct 23 2013
parent "Yura" <min_yura mail.ru> writes:
Thank you very much for your help! I think I start to understand 
it better.

On Wednesday, 23 October 2013 at 14:48:52 UTC, John Colvin wrote:
 On Wednesday, 23 October 2013 at 14:00:46 UTC, Yura wrote:
 Dear all,

 Thank you for your replies!

 Regarding Julia - it seems to be interesting, but - it is too 
 fresh, and from what I understood, it is not compiled. I think 
 D language would be more interesting for me and suitable for 
 my needs (scientific computing).

 Yes, numpy/scipy is OK, but since I have now some time I would 
 like to learn one compiled language which is more close to the 
 hardware,

 "I have done some linear algebra in D. If you are comfortable
 calling C functions, you can easily call into existing 
 solutions,
 because it is trivial to call into C from D."

 This is very interesting since as you know lots of code is 
 written in c. GSL is a good example. The only problem is how 
 to use it. The thing is that i don't know c, but the question 
 is whether I really need to be skilled in c to be able to call 
 c functions. My gut feeling is that no, I don't need to be 
 skilled. I have installed gsl on my computer. But what I need 
 is a good example of a code/codes on how to call this library 
 from d programming language. E.g. I have tried to use gsl. I 
 have written a code in c (simple.c):

 -------
 #include <stdio.h>
 #include <gsl/gsl_sf_bessel.h>

 double fun(double x)
 //main (void)
 {
 //  x = 5.0;
  double y = gsl_sf_bessel_J0 (x);
 //  printf ("J0(%g) = %.18e\n", x, y);
  return y;
 }
 ----------------
 Also, I have written a di file (simple.di):
 ----------------
 extern (C):
 double fun(double);
 ----------------
 And finally, d code (simple.d):
 ------------------
 import std.stdio, std.string, std.array;
 import std.conv;

 import std.stdio;
 import simple;

 void main(){
    writeln( fun(10.0) );
 }
 ------------------------------------

 Unfortunately, when I compile it it says:

 dmd simple.d simple.o
 simple.d(8): Error: undefined identifier fun

 Could one provide a working clear example how to use gsl in D?

 I have tried SciD and it apparently works, though I did not 
 test it so far. I think a tutorial on how to use D in 
 scientific programming would be very appreciated and could 
 attract more people to D.

 PS Thank all of you for helping.
Don't call everything the same name. At the very least don't have the di and d file with the same name. Once you've done that, it will compile but the linker will start to complain. You will need to link to the gsl and gslcblas libraries, making your compilation command this: dmd test.d simple.o -L-lgsl -L-lgslcblas The simplest possible example of using gsl would be this: simpleGSL.d import std.stdio; extern(C) double gsl_sf_bessel_J0(double); void main() { writeln(gsl_sf__bessel_J0(10)); } compile with dmd -L-lgsl -L-lgslcblas simpleGSL.d If you were doing this seriously you would want to create a load of d or di files containing the extern(C) declarations for all the different gsl things you need. Also, you might want to take a look at dstep: https://github.com/jacob-carlborg/dstep which might be able to auto-generate them all for you.
Oct 24 2013