Do more. Code less. Free software for GPU computing.
<scroll to top>

examples/misc/lin_algebra.cpp

#include <stdio.h>
#include <arrayfire.h>

using namespace af;

int main(int argc, char **argv)
{
    try {
        // This example needs DLA license to work.
        printf("\n======= ArrayFire DLA Examples =====\n\n");

        printf("--ArrayFire Eigendecomposition\n\n");
        int n = 3;
        array in;
        array val, vec;

        in = randu(n, n);

        // Eigen values in a vector
        print(eigen(in));

        // Find the Eigen values and Eigen Vectors
        // Eigen values are along the diagonal of a diagonal matrix
        // The corresponding eigen vectors are in the appropriate column
        eigen(val, vec, in);

        // Print Eigen values and Eigen Vectors
        print(in);
        print(val);
        print(vec);

        printf("--ArrayFire LU decomposition\n\n");
        int m = 6;
        n = 4;
        array out;
        array l, u, p;

        in = randu(m, n);
        out = lu(in); // Packed output
        lu(l, u, p, in); // Unpacked output with pivoting

        print(in);
        print(out);
        print(l);
        print(u);
        print(p);

        printf("--ArrayFire Solve\n\n");
        // Generate random data
        n = 6;
        array A = randu(n, n);
        array B = randu(n, n);

        // Solve: A*X = B
        array X = solve(A, B);

        print(A);
        print(B);
        print(X);

        // Find the maximum of absolute error
        float res = max<float>(abs(A * X - B));
        printf("\nMaximum of abolute error: %f\n", res);

    } catch (af::exception& e) {
        fprintf(stderr, "%s\n", e.what());
    }

    #ifdef WIN32 // pause in Windows
    if (!(argc == 2 && argv[1][0] == '-')) {
        printf("hit [enter]...");
        getchar();
    }
    #endif
    return 0;
}
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines