Git Product home page Git Product logo

aimt's Introduction

Aimt

Aimt is an array index mapping tool used to interconvert between index and subscripts. This open-source project is written in C language and performs multidimensional conversions. As we see in Matlab and Octave Forge, there are two functions to mapping array indexes: the sub2ind and ind2sub.

matrix

Getting Started

In the sub2ind and ind2sub functions, the array index traverses rows, then columns, and then higher dimensions. The index starts from 1, and the subscripts start from (1, ..., 1). Right below are basic examples of how these functions work.

sub2ind

Let's use the sub2ind function to get a linear index form of a (4, 3) subscripts in a 5x4 matrix. So, we code:

int myMatrixDimensions[] = {5, 4}; 
int mySubscripts[] = {4, 3}; // index = 14
int arraySize = sizeof(myMatrixDimensions)/sizeof(myMatrixDimensions[0]);
int linearIndex = sub2ind(myMatrixDimensions, mySubscripts, arraySize);

ind2sub

On the other hand, we can use the ind2sub function to move in the opposite way. For example, in a 5x4 matrix, one linear index is 14, and we can get these subscripts form coding like this:

int myMatrixDimensions[] = {5, 4}; 
int myIndex = 14; // subscripts = (4, 3)
int arraySize = sizeof(myMatrixDimensions)/sizeof(myMatrixDimensions[0]);
int mySubscripts[arraySize];
ind2sub(myMatrixDimensions, arraySize, myIndex, mySubscripts);

See the demos folder for complete examples. The Makefile shows how to link and compile all files.

Installation

Keep in mind this tool is under development and was tested only in the Unix environment for the GCC 9.3.0 compiler. If you want to use it, go ahead and copy the src folder into your project and add the aimt.h file by the include directive.

#include "../src/aimt.h"

int main (int argc, char *argv[]){
  // stuff to do...
  return 0;
}

Contributing

I'm so glad when I see improvements done by other people. Feel free to contribute by opening pull requests with improvements, fixing bugs or typing mistakes, or even open an issue to report errors. See some useful stuff in Contributing guide.

License

Aimt is a MIT licensed, as found in the LICENSE file.

aimt's People

Contributors

carltonbranch avatar lobophf avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

carltonbranch

aimt's Issues

Add more tests (Google test library)

The tests that have already written cover only a few cases. That is, those where the user enters correct values ​​in the sub2ind and ind2sub functions. However, the code remains fragile because values ​​outside of ranges have not yet been considered. Therefore, the following list of unit tests is crucial.

  • Add unit tests to sub2ind by predicting errors invoked by the inspectSubscripts function.
  • Add unit tests to ind2sub by predicting errors invoked by the inspectIndex function.
  • Add unit tests to prevent the index from being great than the maximum integer possible.

Update the aimt.h file comments

After inserting macros in the aimt.h file, I forgot to update the comments. These are important for generating Doxygen files. However, working with macros requires additional settings. Soon I will to change the aimt.h file.

Error messages

It would be nice if the error messages showed the document and the line where it occurred. I'll do this with macros.

It might be better to rewrite the existing tests.

Existing tests can be improved because I feel that there are many similar codes.. Perhaps the environment can be rewritten considering the test fixtures. Each test of the sub2ind and ind2sub functions could share the same information to decrease the code lines.

Remember the KISS principle

After rethinking about this project, I don't think that inventing conversions different from the traditional ones. I want to say that I must keep only the two main functions (sub2ind and ind2sub) and remove the other conversion functions.

integer overflow

I made a mistake before regarding the integer overflow. The last element of the arrayOfDimensions must fit in the integer. In other words, I need to compute the maximum product of 9's. This document gave me ideas.

The integer overflow can occur in some cases

The sub2ind function can work with arrays of various dimensions that can certainly return an index value above the limit that an integer can support. Therefore, to prevent the integer overflow, it is necessary to change the sub2ind function so that it returns a long long int.

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.