Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

mir.glas.l1

Level 1

This is a submodule of mir.glas.
The Level 1 GLAS perform vector and vector-vector operations.

Vector-vector operations

rot apply Givens rotation
axpy constant times a vector plus a vector
dot dot product
dotc dot product, conjugating the first vector

Vector operations

Function Name Description
nrm2 Euclidean norm
sqnrm2 square of Euclidean norm
asum sum of absolute values
iamax index of max abs value
amax max abs value
All functions except iamax work with multidimensional tensors.
GLAS does not provide swap, scal, and copy functions. This functionality is part of ndslice package. Examples can be found below.
Authors:
Ilya Yaroshenko
Examples:
SWAP
import std.algorithm.mutation: swap;
import mir.ndslice.allocation: slice;
import mir.algorithm.iteration: each;
import std.typecons: Yes;
auto x = slice!double(4);
auto y = slice!double(4);
x[] = [0, 1, 2, 3];
y[] = [4, 5, 6, 7];
each!(swap)(x, y);
assert(x == [4, 5, 6, 7]);
assert(y == [0, 1, 2, 3]);
Examples:
SCAL
import mir.ndslice.allocation: slice;
import std.typecons: Yes;
auto x = slice!double(4);
x[] = [0, 1, 2, 3];
x[] *= 2.0;
assert(x == [0, 2, 4, 6]);
Examples:
COPY
import mir.ndslice.allocation: slice;
auto x = slice!double(4);
auto y = slice!double(4);
x[] = [0, 1, 2, 3];
y[] = x;
assert(y == [0, 1, 2, 3]);
void rot(C, S, SliceKind kind1, SliceKind kind2, size_t N, Iterator1, Iterator2)(in C c, in S s, Slice!(Iterator1, N, kind1) x, Slice!(Iterator2, N, kind2) y);
Applies a plane rotation, where the c (cos) and s (sin) are scalars. Uses unrolled loops for strides equal to one.
Parameters:
C c cos scalar
S s sin scalar
Slice!(Iterator1, N, kind1) x first n-dimensional tensor
Slice!(Iterator2, N, kind2) y second n-dimensional tensor

BLAS SROT, DROT, CROT, ZROT, CSROT, ZDROTF

Examples:
import mir.ndslice.allocation: slice;
auto x = slice!double(4);
auto y = slice!double(4);
auto a = slice!double(4);
auto b = slice!double(4);
double cos = 3.0 / 5;
double sin = 4.0 / 5;
x[] = [0, 1, 2, 3];
y[] = [4, 5, 6, 7];
foreach (i; 0 .. 4)
{
    a[i] = cos * x[i] + sin * y[i];
    b[i] = cos * y[i] - sin * x[i];
}
rot(cos, sin, x, y);
assert(x == a);
assert(y == b);
void axpy(A, SliceKind kind1, SliceKind kind2, size_t N, Iterator1, Iterator2)(in A a, Slice!(Iterator1, N, kind1) x, Slice!(Iterator2, N, kind2) y);
Constant times a vector plus a vector. Uses unrolled loops for strides equal to one.
Parameters:
A a scale parameter
Slice!(Iterator1, N, kind1) x first n-dimensional tensor
Slice!(Iterator2, N, kind2) y second n-dimensional tensor

BLAS SAXPY, DAXPY, CAXPY, ZAXPY

Examples:
SAXPY, DAXPY
import mir.ndslice.allocation: slice;
auto x = slice!double(4);
auto y = slice!double(4);
x[] = [0, 1, 2, 3];
y[] = [4, 5, 6, 7];
axpy(2.0, x, y);
assert(y == [4, 7, 10, 13]);
Examples:
SAXPY, DAXPY
import mir.ndslice.allocation: slice;

auto a = 3 + 4i;
auto x = slice!cdouble(2);
auto y = slice!cdouble(2);
x[] = [0 + 1i, 2 + 3i];
y[] = [4 + 5i, 6 + 7i];
axpy(a, x, y);
assert(y == [a * (0 + 1i) + (4 + 5i), a * (2 + 3i) + (6 + 7i)]);
F dot(F, SliceKind kind1, SliceKind kind2, size_t N, Iterator1, Iterator2)(Slice!(Iterator1, N, kind1) x, Slice!(Iterator2, N, kind2) y);

auto dot(SliceKind kind1, SliceKind kind2, size_t N, Iterator1, Iterator2)(Slice!(Iterator1, N, kind1) x, Slice!(Iterator2, N, kind2) y);
Forms the dot product of two vectors. Uses unrolled loops for strides equal to one.
Returns:
dot product conj(xᐪ) × y
Parameters:
F type for summation (optional template parameter)
Slice!(Iterator1, N, kind1) x first n-dimensional tensor
Slice!(Iterator2, N, kind2) y second n-dimensional tensor

BLAS SDOT, DDOT, SDSDOT, DSDOT, CDOTC, ZDOTC

Examples:
SDOT, DDOT
import mir.ndslice.allocation: slice;
auto x = slice!double(4);
auto y = slice!double(4);
x[] = [0, 1, 2, 3];
y[] = [4, 5, 6, 7];
assert(dot(x, y) == 5 + 12 + 21);
Examples:
SDOT, DDOT
import mir.ndslice.allocation: slice;
auto x = slice!double(4);
auto y = slice!double(4);
x[] = [0, 1, 2, 3];
y[] = [4, 5, 6, 7];
assert(dot(x, y) == 5 + 12 + 21);
Examples:
SDSDOT, DSDOT
import mir.ndslice.allocation: slice;
auto x = slice!float(4);
auto y = slice!float(4);
x[] = [0, 1, 2, 3];
y[] = [4, 5, 6, 7];
assert(dot!real(x, y) == 5 + 12 + 21); // 80-bit FP for x86 CPUs
Examples:
CDOTU, ZDOTU
import mir.ndslice.allocation: slice;

auto x = slice!cdouble(2);
auto y = slice!cdouble(2);
x[] = [0 + 1i, 2 + 3i];
y[] = [4 + 5i, 6 + 7i];
version(LDC) // DMD Internal error: backend/cgxmm.c 628
assert(dot(x, y) == (0 + 1i) * (4 + 5i) + (2 + 3i) * (6 + 7i));
F dotc(F, SliceKind kind1, SliceKind kind2, size_t N, Iterator1, Iterator2)(Slice!(Iterator1, N, kind1) x, Slice!(Iterator2, N, kind2) y)
if (isComplex!(DeepElementType!(typeof(x))) && isComplex!(DeepElementType!(typeof(y))));

auto dotc(SliceKind kind1, SliceKind kind2, size_t N, Iterator1, Iterator2)(Slice!(Iterator1, N, kind1) x, Slice!(Iterator2, N, kind2) y);
Forms the dot product of two complex vectors. Uses unrolled loops for strides equal to one.
Returns:
dot product xᐪ × y
Parameters:
F type for summation (optional template parameter)
Slice!(Iterator1, N, kind1) x first n-dimensional tensor
Slice!(Iterator2, N, kind2) y second n-dimensional tensor

BLAS CDOTU, ZDOTU

Examples:
CDOTC, ZDOTC
import mir.ndslice.allocation: slice;

auto x = slice!cdouble(2);
auto y = slice!cdouble(2);
x[] = [0 + 1i, 2 + 3i];
y[] = [4 + 5i, 6 + 7i];
version(LDC) // DMD Internal error: backend/cgxmm.c 628
assert(dotc(x, y) == (0 + -1i) * (4 + 5i) + (2 + -3i) * (6 + 7i));
F nrm2(F, SliceKind kind, size_t N, Iterator)(Slice!(Iterator, N, kind) x);

auto nrm2(SliceKind kind, size_t N, Iterator)(Slice!(Iterator, N, kind) x);
Returns the euclidean norm of a vector. Uses unrolled loops for stride equal to one.
Returns:
euclidean norm sqrt(conj(xᐪ) × x)
Parameters:
F type for summation (optional template parameter)
Slice!(Iterator, N, kind) x n-dimensional tensor

BLAS SNRM2, DNRM2, SCNRM2, DZNRM2

Examples:
SNRM2, DNRM2
import mir.ndslice.allocation: slice;
import std.math: sqrt, approxEqual;
auto x = slice!double(4);
x[] = [0, 1, 2, 3];
assert(nrm2(x).approxEqual(sqrt(1.0 + 4 + 9)));
Examples:
SCNRM2, DZNRM2
import mir.ndslice.allocation: slice;
import std.math: sqrt, approxEqual;

auto x = slice!cdouble(2);
x[] = [0 + 1i, 2 + 3i];

assert(nrm2(x).approxEqual(sqrt(1.0 + 4 + 9)));
F sqnrm2(F, SliceKind kind, size_t N, Iterator)(Slice!(Iterator, N, kind) x);

auto sqnrm2(SliceKind kind, size_t N, Iterator)(Slice!(Iterator, N, kind) x);
Forms the square of the euclidean norm. Uses unrolled loops for stride equal to one.
Returns:
conj(xᐪ) × x
Parameters:
F type for summation (optional template parameter)
Slice!(Iterator, N, kind) x n-dimensional tensor
Examples:
import mir.ndslice.allocation: slice;
auto x = slice!double(4);
x[] = [0, 1, 2, 3];
assert(sqnrm2(x) == 1.0 + 4 + 9);
Examples:
import mir.ndslice.allocation: slice;

auto x = slice!cdouble(2);
x[] = [0 + 1i, 2 + 3i];

assert(sqnrm2(x) == 1.0 + 4 + 9);
F asum(F, SliceKind kind, size_t N, Iterator)(Slice!(Iterator, N, kind) x);

auto asum(SliceKind kind, size_t N, Iterator)(Slice!(Iterator, N, kind) x);
Takes the sum of the |Re(.)| + |Im(.)|'s of a vector and returns a single precision result.
Returns:
sum of the |Re(.)| + |Im(.)|'s
Parameters:
F type for summation (optional template parameter)
Slice!(Iterator, N, kind) x n-dimensional tensor

BLAS SASUM, DASUM, SCASUM, DZASUM

Examples:
SASUM, DASUM
import mir.ndslice.allocation: slice;
auto x = slice!double(4);
x[] = [0, -1, -2, 3];
assert(asum(x) == 1 + 2 + 3);
Examples:
SCASUM, DZASUM
import mir.ndslice.allocation: slice;

auto x = slice!cdouble(2);
x[] = [0 - 1i, -2 + 3i];

assert(asum(x) == 1 + 2 + 3);
sizediff_t iamax(Iterator, SliceKind kind)(Slice!(Iterator, 1, kind) x);
Finds the index of the first element having maximum |Re(.)| + |Im(.)|.

Return index of the first element having maximum |Re(.)| + |Im(.)|

Parameters:
Slice!(Iterator, 1, kind) x 1-dimensional tensor

BLAS ISAMAX, IDAMAX, ICAMAX, IZAMAX

Examples:
ISAMAX, IDAMAX
import mir.ndslice.allocation: slice;
auto x = slice!double(6);
//     0  1   2   3   4  5
x[] = [0, -1, -2, -3, 3, 2];
assert(iamax(x) == 3);
// -1 for empty vectors
assert(iamax(x[0 .. 0]) == -1);
Examples:
ICAMAX, IZAMAX
import mir.ndslice.allocation: slice;

auto x = slice!cdouble(4);
//        0          1          2         3
x[] = [0 + -1i, -2 + 3i, 2 + 3i, 2 + 2i];

assert(iamax(x) == 1);
// -1 for empty vectors
assert(iamax(x[$ .. $]) == -1);
auto amax(SliceKind kind, size_t N, Iterator)(Slice!(Iterator, N, kind) x);
Takes the sum of the |Re(.)| + |Im(.)|'s of a vector and returns a single precision result.
Returns:
sum of the |Re(.)| + |Im(.)|'s
Parameters:
Slice!(Iterator, N, kind) x n-dimensional tensor

BLAS SASUM, DASUM, SCASUM, DZASUM

Examples:
import mir.ndslice.allocation: slice;
auto x = slice!double(6);
x[] = [0, -1, -2, -7, 6, 2];
assert(amax(x) == 7);
// 0 for empty vectors
assert(amax(x[0 .. 0]) == 0);
Examples:
import mir.ndslice.allocation: slice;

auto x = slice!cdouble(4);
x[] = [0 + -1i, -7 + 3i, 2 + 3i, 2 + 2i];

assert(amax(x) == 10);
// 0 for empty vectors
assert(amax(x[$ .. $]) == 0);