Giter Club home page Giter Club logo

mdindex's Introduction

mdindex

A C++ implement of nested loop with variable dimension

For real life computational research, multi-dimension problems kill our time.

Consider an N-dimension problem. We write some codes with N level loops.

A month later, M-dimension problems need to be solved. All you can do is to rewrite the codes.

To deal with this problem, I came up with this solution. Coordinates in such case can be stored in a vector object. A non-nested loop then can be used if the vector itself can loop over all dimensions.

The tool present here is a pure head file. Just include it in your source file, and it should work.

First thing you should notice is that the work based on the standard container vector. Learn more about vector class if you are not familiar with it.

In this work, mdindex class is implemented. To use this class, include the head file

#include "path/to/mdindex.h"

The method listed below:

Initialization

mdindex(const std::vector <int> &max);

Set the integer in max as the largest number of each dimension. For example, you have

std::vector<int> max{3,4,5};

This means here we have a three-dimension problem where the number of discrete points in each dimension is 3, 4, or 5, respectively.

Then you can write

mdindex in(max);

to have a multi-dimension index object.

mdindex(const mdindex& rhs);

The make a copy of rhs.

Set the status

mdindex::reset();

Set all the indices to 0

Change the index

  • ++ -- operators. To the next or previous one.
  • += -= operators. Run more steps.

You should notice as we are using C++, the index runs from 0 to (max-1). If it runs out of range, a runtime error is thrown. However, I choose not stop the program for some reason. For example, you type

for(mdindex in(max); in.get()!=in.max(); ++i);

This will work, but at the last cycle, a warning will be thrown.

You can also use in.overflow() to find whether it is overflowed. It is safe only when this value equals 0!

Check the current index

mdindex::get();

returns the current overall index.

mdindex::get(unsigned int i);

returns the current ith dimension index.

mdindex::max();

returns the total number of possible combination.

mdindex::max(unsigned int i);

returns the max ith dimension index.

mdindex's People

Contributors

zhaiyusci avatar

Stargazers

 avatar  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.