Coder Social home page Coder Social logo

tensor's Introduction

tensor

this is a small c++ program to generate tensor classes up to an arbitrary rank; use the file "tensor.hpp" from this repo or compile "generate-tensor.cpp" yourself to generate "tensor.hpp";

examples:

  1. simple 1D-array

Tensor1 f(10); for (int x = 0; x < 10; ++x) { f(x) = 5.0 * x; }

  1. arbitrary index range

Tensor1 f(-10, +10); for (int x = -10; x <= 10; ++x) { f(x) = 5.0 * x; }

  1. multiple indices (non-physicist see wikipedia for the epsilon-tensor or levi-civita-symbol)

Tensor3 epsilon(1, 3, 1, 3, 1, 3); // each of the 3 epsilon-indices goes from 1 to 3 for (int i = 1; i <= 3; ++i) { for (int j = 1; j <= 3; ++j) { for (int k = 1; k <= 3; ++k) { if (i == j || i == k || j == k) { epsilon(i, j, k) = 0; } else if (i == 1 && j == 2 && k == 3 || i == 2 && j == 3 && k == 1 || i == 3 && j == 1 && k == 2) { epsilon(i, j, k) = 1; } else { epsilon(i, j, k) = -1; } } } }

  1. fix first indices

Tensor2 a(10, 10); for (int i = 0; i < 10; ++i) { for (int j = 0; j < 10; ++j) { a(i, j) = i * j; } } TensorBase1 b = a(5); for (int i = 0; i < 10; ++i) { cout << b(i) << endl; // prints a(5, 0), a(5, 1), ... }

  1. get index range

Tensor2 a(-10, +10, -5, +5); // a(x, y) with -10 <= x <= +10 and -5 <= y +5 int x0 = f.baseIndex(0); int x1 = x0 + f.size(0) - 1; int y0 = f.baseIndex(1); int y1 = y0 + f.size(1) - 1; for (int x = x0; x <= x1; ++x) { for (int y = y0; y <= y1; ++y) { a(x, y) = x + y; } }

  1. resize

Tensor2 a(10, 15); // a(x, y) with 0 <= x < 10 and 0 <= y < 15 ... a.resize(20, 25); // now a(x, y) with 0 <= x < 20 and 0 <= y < 25 ... a.resize(-10, +10, -20, +20); // now a(x, y) with -10 <= x <= +10 and -20 <= x <= +20

tensor's People

Contributors

kingfisher1337 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.