| 1 |
// Written in the D programming language 1.0. |
|---|
| 2 |
/** |
|---|
| 3 |
* BLADE Alpha -- Basic Linear Algebra D Expressions |
|---|
| 4 |
* |
|---|
| 5 |
*/ |
|---|
| 6 |
|
|---|
| 7 |
module BladeDemo; |
|---|
| 8 |
import blade.Blade; |
|---|
| 9 |
import std.stdio; |
|---|
| 10 |
|
|---|
| 11 |
// Local arrays in D aren't currently aligned to 128-bit boundaries. |
|---|
| 12 |
// In such cases, the library generates an 'SSE misalignment' assert error, |
|---|
| 13 |
// to avoid segfaults. |
|---|
| 14 |
// Use heap-allocated arrays, or static arrays (DMD 1.023 or later) |
|---|
| 15 |
// cdouble[] always remains aligned, even when sliced. |
|---|
| 16 |
|
|---|
| 17 |
void main() |
|---|
| 18 |
{ |
|---|
| 19 |
static z = [3.4, 565, 31.3, 0]; |
|---|
| 20 |
double [] a = new double[4]; |
|---|
| 21 |
double [] d = [0.5, 2.8, 17.0, 28.25, 1, 56.2, 3.4]; |
|---|
| 22 |
a[0..$] = [3.4, 565, 31.3, 41.8]; |
|---|
| 23 |
auto w2=z.ptr; |
|---|
| 24 |
float[] q = new float[4]; |
|---|
| 25 |
q[0..$]= [17.0f, 28.25, 1, 0]; |
|---|
| 26 |
float [4] r; |
|---|
| 27 |
real [] p = [2.3, 254, 0.1, 1.2]; |
|---|
| 28 |
for(int i=0; i<r.length;++i) { |
|---|
| 29 |
r[i]= q[i]*2213.3L; |
|---|
| 30 |
} |
|---|
| 31 |
double [4][] another = [[33.1, 4543, 43, 878.7], |
|---|
| 32 |
[5.14, 455, 554, 2.43]]; |
|---|
| 33 |
real k=3.4; |
|---|
| 34 |
mixin(vectorize(` a += (d[2..$-1]*2.01*a[2]-another[][1])["abc".length-3..$]`)); |
|---|
| 35 |
mixin(vectorize(" a-= 2.01*( 3.04+k)*r")); |
|---|
| 36 |
|
|---|
| 37 |
mixin(vectorize("q+= q*2.01")); |
|---|
| 38 |
mixin(vectorize("another[0]+=r-=another[0]+another[0]")); |
|---|
| 39 |
|
|---|
| 40 |
// All of the next four are equivalent |
|---|
| 41 |
mixin(vectorize("a+=6*another[1,0..$]")); |
|---|
| 42 |
mixin(vectorize("a+=6*(another[1,0..$]+another[1,0..$])")); |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
mixin(vectorize("a+=6*another[1][0..$]")); |
|---|
| 46 |
mixin(vectorize("a+=6*another[1]")); |
|---|
| 47 |
mixin(vectorize("a+=6*another[1][]")); |
|---|
| 48 |
// I don't think I'll support this syntax long-term. |
|---|
| 49 |
mixin(vectorize("a+=6*another[1,[0,$]]")); |
|---|
| 50 |
|
|---|
| 51 |
// Strided vector |
|---|
| 52 |
mixin(vectorize("another[0..$,1]=6*a[0..2]")); |
|---|
| 53 |
|
|---|
| 54 |
// Simplifies to q*= 2*dot(q,q)*dot(q,q). |
|---|
| 55 |
mixin(vectorize("q *=dot(q,q*dot(2*q,q))")); |
|---|
| 56 |
double u; |
|---|
| 57 |
mixin(vectorize("u = dot(q,q*dot(q,q))")); |
|---|
| 58 |
mixin(vectorize("u = dot(a, q)")); |
|---|
| 59 |
mixin(vectorize("u = sum(abs(-p))")); |
|---|
| 60 |
mixin(vectorize("a = -a")); |
|---|
| 61 |
mixin(vectorize("u = sum(sqrt(abs(p))) + sum(sqrt(abs(q)))")); |
|---|
| 62 |
mixin(vectorize("u = prod(q)")); |
|---|
| 63 |
writefln("a=", a); |
|---|
| 64 |
|
|---|
| 65 |
} |
|---|