www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - library to solve the system of linear equations

reply Yura <yuriy.min gmail.com> writes:
Dear All,

I am very new to D, and it has been a while since I coded in 
anything than Python. I am using just notepad along with the gdc 
compiler.

At the moment I need to solve the system of liner equations:

A00*q0 + A01*q1 + A02*q2 ... = -V0
A10*q0 + A11*q1 + A12*q2 ... = -V1
...

I have all my Aij coefficients in the double [][] A array, and V 
numbers in the array double [] b.

Is there any quick D solution for it? Something like in python 
one can do:

numpy.linalg.solve(a,b) ?

Quick web search revealed a few scientific libraries for D like 
Mir or lubek.

I am working under Ubuntu 18.04 and compiling my code like " gdc 
solv.d"

Could someone please provide a quick d solution for this, 
including details on how to install the library & link it to the 
code? I do not need anything advanced, just the easiest option.

I am sorry for too basic question, just want to check my idea 
quickly. Thank you!
Oct 14 2022
next sibling parent Ben Jones <fake fake.fake> writes:
On Friday, 14 October 2022 at 17:41:42 UTC, Yura wrote:
 ...
Check out MIR https://github.com/libmir
Oct 14 2022
prev sibling next sibling parent M.M. <matus email.cz> writes:
On Friday, 14 October 2022 at 17:41:42 UTC, Yura wrote:
 Dear All,

 I am very new to D, and it has been a while since I coded in 
 anything than Python. I am using just notepad along with the 
 gdc compiler.

 [...]
Did not try it, but the example for the function luSolve at the lubeck documentation website should be helpful: https://lubeck.dpldocs.info/v1.5.1/kaleidic.lubeck.luSolve.html
Oct 14 2022
prev sibling next sibling parent reply Sergey <kornburn yandex.ru> writes:
On Friday, 14 October 2022 at 17:41:42 UTC, Yura wrote:
 Dear All,

 I am very new to D, and it has been a while since I coded in 
 anything than Python. I am using just notepad along with the 
 gdc compiler.

 At the moment I need to solve the system of liner equations:

 A00*q0 + A01*q1 + A02*q2 ... = -V0
 A10*q0 + A11*q1 + A12*q2 ... = -V1
 ...

 Could someone please provide a quick d solution for this, 
 including details on how to install the library & link it to 
 the code? I do not need anything advanced, just the easiest 
 option.

 I am sorry for too basic question, just want to check my idea 
 quickly. Thank you!
Firstly I will suggest to install dub - the D package manager. It will be easier to isntall other libraries using it. Install openblas in your Ubuntu system. After that you can create a new folder and type in terminal ``` dub init ``` The main part will be about "dependencies". Type "lubeck". After that you could check the example of the simple program here: https://github.com/kaleidicassociates/lubeck/blob/master/example/source/app.d It is exactly solving simple system of linear equations. Link to the package where you can find additional details and documentation: https://code.dlang.org/packages/lubeck
Oct 14 2022
parent reply Yura <yuriy.min gmail.com> writes:
On Friday, 14 October 2022 at 18:37:00 UTC, Sergey wrote:
 On Friday, 14 October 2022 at 17:41:42 UTC, Yura wrote:
 Dear All,

 I am very new to D, and it has been a while since I coded in 
 anything than Python. I am using just notepad along with the 
 gdc compiler.

 At the moment I need to solve the system of liner equations:

 A00*q0 + A01*q1 + A02*q2 ... = -V0
 A10*q0 + A11*q1 + A12*q2 ... = -V1
 ...

 Could someone please provide a quick d solution for this, 
 including details on how to install the library & link it to 
 the code? I do not need anything advanced, just the easiest 
 option.

 I am sorry for too basic question, just want to check my idea 
 quickly. Thank you!
Firstly I will suggest to install dub - the D package manager. It will be easier to isntall other libraries using it. Install openblas in your Ubuntu system. After that you can create a new folder and type in terminal ``` dub init ``` The main part will be about "dependencies". Type "lubeck". After that you could check the example of the simple program here: https://github.com/kaleidicassociates/lubeck/blob/master/example/source/app.d It is exactly solving simple system of linear equations. Link to the package where you can find additional details and documentation: https://code.dlang.org/packages/lubeck
Thank you! First, I installed dub on Linux. Then I installed both libopenblas-dev and libopenblas-base. After that I tried to initiate a new project via dub init. I tried to use the following dependency: "mir-algorithm". After I got dub.sdl file that contains at the end line: dependency "mir-algorithm" version="~>3.16.12" in the top of my el.d file I have: /+dub.sdl: dependency "mir-algorithm" version="~>3.16.12" +/ import std.stdio; import std.string; import std.conv; import std.exception : assertThrown; import std.math; import mir.ndslice; however, when I try to compile it (gdc el.d) it gives me the following error message: el.d:11:8: error: module ndslice is in file 'mir/ndslice.d' which cannot be read import mir.ndslice; ^ import path[0] = /usr/lib/gcc/x86_64-linux-gnu/8/include/d What am I doing wrong? Should I specify the library upon compilation? I am sorry, I am feeling I am asking too basic question. Thank you!
Oct 14 2022
next sibling parent Sergey <kornburn yandex.ru> writes:
On Friday, 14 October 2022 at 21:38:45 UTC, Yura wrote:
 On Friday, 14 October 2022 at 18:37:00 UTC, Sergey wrote:
 however, when I try to compile it (gdc el.d) it gives me the 
 following error message:

 el.d:11:8: error: module ndslice is in file 'mir/ndslice.d' 
 which cannot be read
  import mir.ndslice;
         ^
 import path[0] = /usr/lib/gcc/x86_64-linux-gnu/8/include/d

 What am I doing wrong? Should I specify the library upon 
 compilation?

 I am sorry, I am feeling I am asking too basic question.

 Thank you!
Did you try to use 'dub build'? You should run it in the same folder where dub.sdl is located. Also maybe you will have to specify compiler in dub: ``` dub build --compiler=gdc ``` Some details about DUB you could find here https://dub.pm/
Oct 15 2022
prev sibling parent reply Siarhei Siamashka <siarhei.siamashka gmail.com> writes:
On Friday, 14 October 2022 at 21:38:45 UTC, Yura wrote:
 in the top of my el.d file I have:
 ```D
 /+dub.sdl:
 dependency "mir-algorithm" version="~>3.16.12"
 +/
 import std.stdio;
 import std.string;
 import std.conv;
 import std.exception : assertThrown;
 import std.math;
 import mir.ndslice;
 ```

 however, when I try to compile it (gdc el.d) it gives me the 
 following error message:

 el.d:11:8: error: module ndslice is in file 'mir/ndslice.d' 
 which cannot be read
  import mir.ndslice;
This all looks good, except for the `"gdc el.d"` part. A small application contained in a single .d file can be run like a script via `"dub el.d"`. This is explained at https://dub.pm/advanced_usage.html For compiling highly optimized final binaries you can run `"dub build --build=release --single --compiler=ldc2 el.d"` (to use LDC) or `"dub build --build=release --single --compiler=gdc el.d"` (to use GDC).
Oct 15 2022
parent reply Yura <yuriy.min gmail.com> writes:
Dear All, Thank you so much for your replies and hints! I got it 
working today. All the libraries are properly linked and the 
Equation solver runs smoothly.

The compilers turned out to be problematic though. The "Mir" 
library does not work with the Ubuntu 18.04 gdc and ldc 
compilers. I have managed to install the latest version dmd, and 
it works. But I suspect that the dmd compiler is not optimal in 
terms of performance. The question becomes whether it is possible 
to install the most recent ldc and gdc compilers on Ubuntu 18.04?

The good thing is that I have also managed to get this code 
working in parallel.

Thank you again!

On Saturday, 15 October 2022 at 13:11:04 UTC, Siarhei Siamashka 
wrote:
 On Friday, 14 October 2022 at 21:38:45 UTC, Yura wrote:
 in the top of my el.d file I have:
 ```D
 /+dub.sdl:
 dependency "mir-algorithm" version="~>3.16.12"
 +/
 import std.stdio;
 import std.string;
 import std.conv;
 import std.exception : assertThrown;
 import std.math;
 import mir.ndslice;
 ```

 however, when I try to compile it (gdc el.d) it gives me the 
 following error message:

 el.d:11:8: error: module ndslice is in file 'mir/ndslice.d' 
 which cannot be read
  import mir.ndslice;
This all looks good, except for the `"gdc el.d"` part. A small application contained in a single .d file can be run like a script via `"dub el.d"`. This is explained at https://dub.pm/advanced_usage.html For compiling highly optimized final binaries you can run `"dub build --build=release --single --compiler=ldc2 el.d"` (to use LDC) or `"dub build --build=release --single --compiler=gdc el.d"` (to use GDC).
Oct 17 2022
next sibling parent reply mw <mingwu gmail.com> writes:
On Monday, 17 October 2022 at 19:54:12 UTC, Yura wrote:

 it is possible to install the most recent ldc and gdc compilers 
 on Ubuntu 18.04?
Yes, I used LDC on the same system.
Oct 17 2022
parent reply Siarhei Siamashka <siarhei.siamashka gmail.com> writes:
On Monday, 17 October 2022 at 20:05:24 UTC, mw wrote:
 On Monday, 17 October 2022 at 19:54:12 UTC, Yura wrote:

 it is possible to install the most recent ldc and gdc 
 compilers on Ubuntu 18.04?
Yes, I used LDC on the same system.
What's the recommended way to have up to date D compilers in Ubuntu? I'm using Gentoo Linux and there's a 'dlang' overlay for various recent versions of DMD and LDC. I can also compile any version of GDC myself either via portage or just from a GCC sources tarball or git.
Oct 18 2022
parent reply mw <mingwu gmail.com> writes:
On Tuesday, 18 October 2022 at 09:56:09 UTC, Siarhei Siamashka 
wrote:
 On Monday, 17 October 2022 at 20:05:24 UTC, mw wrote:
 On Monday, 17 October 2022 at 19:54:12 UTC, Yura wrote:

 it is possible to install the most recent ldc and gdc 
 compilers on Ubuntu 18.04?
Yes, I used LDC on the same system.
What's the recommended way to have up to date D compilers in Ubuntu?
I just download the official prebuilt binary from the ldc github repo.
Oct 18 2022
parent reply Yura <yuriy.min gmail.com> writes:
Yes, did the same and it worked. The amazing thing is that the 
system solver turned out to be natively parallel and runs 
smoothly!

On Tuesday, 18 October 2022 at 15:22:02 UTC, mw wrote:
 On Tuesday, 18 October 2022 at 09:56:09 UTC, Siarhei Siamashka 
 wrote:
 On Monday, 17 October 2022 at 20:05:24 UTC, mw wrote:
 On Monday, 17 October 2022 at 19:54:12 UTC, Yura wrote:

 it is possible to install the most recent ldc and gdc 
 compilers on Ubuntu 18.04?
Yes, I used LDC on the same system.
What's the recommended way to have up to date D compilers in Ubuntu?
I just download the official prebuilt binary from the ldc github repo.
Oct 18 2022
parent reply Yura <yuriy.min gmail.com> writes:
I am now trying to compile the code statically using the dub 
manager via the following command line:

dub build --force --build=release --compiler=path_to_ldc2/ldc2

and having these lines in my dub.sdl file:

dependency "mir" version="~>3.2.3"
dependency "lubeck" version="~>1.5.1"
dflags "-static"

For some reasons it does not work:

(.text+0x2f2): undefined reference to `_gfortran_concat_string'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libopenblas.a(dorm
q.o):(.text+0x784): more undefined references to `_gfortran_concat_string'
follow
collect2: error: ld returned 1 exit status
Error: /usr/bin/cc failed with status: 1

Any idea of what I am doing wrong? Should I also specify 
something in my dub.sdl via lflags?

Thank you in advance!

On Tuesday, 18 October 2022 at 17:01:53 UTC, Yura wrote:
 Yes, did the same and it worked. The amazing thing is that the 
 system solver turned out to be natively parallel and runs 
 smoothly!

 On Tuesday, 18 October 2022 at 15:22:02 UTC, mw wrote:
 On Tuesday, 18 October 2022 at 09:56:09 UTC, Siarhei Siamashka 
 wrote:
 On Monday, 17 October 2022 at 20:05:24 UTC, mw wrote:
 On Monday, 17 October 2022 at 19:54:12 UTC, Yura wrote:

 it is possible to install the most recent ldc and gdc 
 compilers on Ubuntu 18.04?
Yes, I used LDC on the same system.
What's the recommended way to have up to date D compilers in Ubuntu?
I just download the official prebuilt binary from the ldc github repo.
Oct 26 2022
parent reply Yura <yuriy.min gmail.com> writes:
OK, got the problem solved by adding the following lines in my 
dub.sdl file:

lflags "-lopenblas" "-lgfortran"
dflags "--static"

However, one problem still remains. Apparently, when compiled 
with dub --release mode I got segfault in my try - catch block. 
Any solution to this?


On Wednesday, 26 October 2022 at 12:24:47 UTC, Yura wrote:
 I am now trying to compile the code statically using the dub 
 manager via the following command line:

 dub build --force --build=release --compiler=path_to_ldc2/ldc2

 and having these lines in my dub.sdl file:

 dependency "mir" version="~>3.2.3"
 dependency "lubeck" version="~>1.5.1"
 dflags "-static"

 For some reasons it does not work:

 (.text+0x2f2): undefined reference to `_gfortran_concat_string'
 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libopenblas.a(dorm
q.o):(.text+0x784): more undefined references to `_gfortran_concat_string'
follow
 collect2: error: ld returned 1 exit status
 Error: /usr/bin/cc failed with status: 1

 Any idea of what I am doing wrong? Should I also specify 
 something in my dub.sdl via lflags?

 Thank you in advance!

 On Tuesday, 18 October 2022 at 17:01:53 UTC, Yura wrote:
 Yes, did the same and it worked. The amazing thing is that the 
 system solver turned out to be natively parallel and runs 
 smoothly!

 On Tuesday, 18 October 2022 at 15:22:02 UTC, mw wrote:
 On Tuesday, 18 October 2022 at 09:56:09 UTC, Siarhei 
 Siamashka wrote:
 On Monday, 17 October 2022 at 20:05:24 UTC, mw wrote:
 On Monday, 17 October 2022 at 19:54:12 UTC, Yura wrote:

 it is possible to install the most recent ldc and gdc 
 compilers on Ubuntu 18.04?
Yes, I used LDC on the same system.
What's the recommended way to have up to date D compilers in Ubuntu?
I just download the official prebuilt binary from the ldc github repo.
Oct 26 2022
parent Yura <yuriy.min gmail.com> writes:
The workaround is to compile without --release mode, but using 
the "O3" ldc flag instead does the job - the binary is fast, yet 
the try - catch block executes properly.

On Wednesday, 26 October 2022 at 20:13:37 UTC, Yura wrote:
 OK, got the problem solved by adding the following lines in my 
 dub.sdl file:

 lflags "-lopenblas" "-lgfortran"
 dflags "--static"

 However, one problem still remains. Apparently, when compiled 
 with dub --release mode I got segfault in my try - catch block. 
 Any solution to this?


 On Wednesday, 26 October 2022 at 12:24:47 UTC, Yura wrote:
 I am now trying to compile the code statically using the dub 
 manager via the following command line:

 dub build --force --build=release --compiler=path_to_ldc2/ldc2

 and having these lines in my dub.sdl file:

 dependency "mir" version="~>3.2.3"
 dependency "lubeck" version="~>1.5.1"
 dflags "-static"

 For some reasons it does not work:

 (.text+0x2f2): undefined reference to `_gfortran_concat_string'
 /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libopenblas.a(dorm
q.o):(.text+0x784): more undefined references to `_gfortran_concat_string'
follow
 collect2: error: ld returned 1 exit status
 Error: /usr/bin/cc failed with status: 1

 Any idea of what I am doing wrong? Should I also specify 
 something in my dub.sdl via lflags?

 Thank you in advance!

 On Tuesday, 18 October 2022 at 17:01:53 UTC, Yura wrote:
 Yes, did the same and it worked. The amazing thing is that 
 the system solver turned out to be natively parallel and runs 
 smoothly!

 On Tuesday, 18 October 2022 at 15:22:02 UTC, mw wrote:
 On Tuesday, 18 October 2022 at 09:56:09 UTC, Siarhei 
 Siamashka wrote:
 On Monday, 17 October 2022 at 20:05:24 UTC, mw wrote:
 On Monday, 17 October 2022 at 19:54:12 UTC, Yura wrote:

 it is possible to install the most recent ldc and gdc 
 compilers on Ubuntu 18.04?
Yes, I used LDC on the same system.
What's the recommended way to have up to date D compilers in Ubuntu?
I just download the official prebuilt binary from the ldc github repo.
Oct 26 2022
prev sibling parent reply jmh530 <john.michael.hall gmail.com> writes:
On Monday, 17 October 2022 at 19:54:12 UTC, Yura wrote:
 Dear All, Thank you so much for your replies and hints! I got 
 it working today. All the libraries are properly linked and the 
 Equation solver runs smoothly.

 The compilers turned out to be problematic though. The "Mir" 
 library does not work with the Ubuntu 18.04 gdc and ldc 
 compilers. I have managed to install the latest version dmd, 
 and it works. But I suspect that the dmd compiler is not 
 optimal in terms of performance. The question becomes whether 
 it is possible to install the most recent ldc and gdc compilers 
 on Ubuntu 18.04?

 [snip]
If you have a problem with support for mir, submit a bug report. I don't think gdc is supported, but ldc should be.
Oct 17 2022
next sibling parent reply mw <mingwu gmail.com> writes:
On Monday, 17 October 2022 at 20:22:47 UTC, jmh530 wrote:
 If you have a problem with support for mir, submit a bug 
 report. I don't think gdc is supported, but ldc should be.
The latest version of Mir can only be compiled with latest Ldc 1.30, 1.29 doesn't work. Maybe Mir should add static check for supported complier versions, rather than let user try and error.
Oct 17 2022
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 18/10/2022 9:37 AM, mw wrote:
 Maybe Mir should add static check for supported complier versions, 
 rather than let user try and error.
Dub has dependency checks for compiler/dub in it. It doesn't need to be in code.
Oct 17 2022
parent mw <mingwu gmail.com> writes:
On Monday, 17 October 2022 at 20:39:10 UTC, rikki cattermole 
wrote:
 On 18/10/2022 9:37 AM, mw wrote:
 Maybe Mir should add static check for supported complier 
 versions, rather than let user try and error.
Dub has dependency checks for compiler/dub in it. It doesn't need to be in code.
Not everyone use dub to build.
Oct 17 2022
prev sibling parent Siarhei Siamashka <siarhei.siamashka gmail.com> writes:
On Monday, 17 October 2022 at 20:22:47 UTC, jmh530 wrote:
 If you have a problem with support for mir, submit a bug 
 report. I don't think gdc is supported, but ldc should be.
GDC12 has finally upgraded its D language frontend version to 2.100 and I have successfully compiled a simple lubeck example by it after applying this patch: https://github.com/libmir/mir-core/pull/72 (it's merged now and the next version of mir-core will probably have it included). But GDC isn't officially supported yet and isn't even getting tested by the current libmir's CI pipeline. Maybe GDC12 can be added to it now? There's an open issue about troubles with LDC 1.24.0 from Debian 11: https://github.com/libmir/mir-core/issues/64 Libmir developers seem to be very eager to try every new language feature and break compatibility with D compilers unless these compilers are always super fresh. That said, it's still possible to specify older versions of mir libraries in dub.sdl and compile the lubeck example by LDC 1.24.0 or DMD 2.091.1 with something like this: ```D /+dub.sdl: dependency "lubeck" version="==1.5.1" dependency "mir-core" version="==1.1.85" dependency "mir-algorithm" version="==3.11.7" +/ ```
Oct 18 2022
prev sibling parent Siarhei Siamashka <siarhei.siamashka gmail.com> writes:
On Friday, 14 October 2022 at 17:41:42 UTC, Yura wrote:
 I am working under Ubuntu 18.04 and compiling my code like "gdc 
 solv.d"
That's an old distribution released 4 years ago and already approaching its End of Standard Support in a few months (April 2023). This distribution offers old versions of D compilers too: * LDC 1.8.0-1 (compatible with old DMD 2.078) * GDC 8.3.0-1ubuntu2.3 (compatible with old DMD 2.068) If you are really forced to choose between these two, then LDC is likely a better option. You can probably also install DMD from Snap Store: https://snapcraft.io/install/dmd/ubuntu
Oct 15 2022