Git Product home page Git Product logo

noursmath's Introduction

noursmath

Collection of various Math and Linear Algebra utilities, mainly for use on matrices.

All "matrices" in noursmath as well as DARRs are simple arrays, not array of arrays, not multidimensional arrays. Higher-dimensions are mapped unto simple arrays by indexing, for example in 2D to access element at (row, col) you write array[row * col_len + col].

Originally created for use in a game I am developping using C99: Codename Firesaga. Title pending.

Noursmath contains 3 modules: q_math (quick math), linalg (linear algebra) and pathfinding.

Installation

Add nmath.c and nmath.h to your source code.

Motivation

Make a very simple math/linear algebra library, with fast operations. Use simple arrays everywhere, map higher dimensions by indexing.
Compileable with tcc.

Features

  • Very simple.
  • Naive operations, no overflow protection, wonky unsigned types.
  • Includes DARR and DTAB.
  • Fast? Questionable (no vectorization)
  • No memory management. Caller deals with memory.

q_math

For now, only q_sqrt (otherwise known as the carmack sqrt) is truly a "quick" function. Otherwise this module contains macros and functions that produce useful sequences of integers. For example, the macro q_cycle4_mppm(i) creates the periodic cycle -1, +1, +1, -1... for i++, starting from i = 0.

Similarly, the macro q_cycle3_zpm(i) outputs 0, +1, -1...

Also, q_sequence_pingpong(i, 0, 3) outputs 0, 1, 2, 1, 0, 1, 2... for i++ starting from i = 0.

Further, i = q_sequence_geometric(i, 1, 2) outputs 32, 16, 8, 4, 2, 1, 1.... starting from i = 32.

linalg

This module contains various linear algebra functions, mainly element-wise addition, multiplication and boolean operation for arrays (including n-dim matrices). Common linear algebra operations like dot product and cross product are implemented for 2D matrices.

pathfinding

These functions are designed for a turn-based strategy game that takes place on a 2D map with square tiles, made to compute movement and sight. Some variants are implemented for a 2D map with hexagonal tiles.

No diagonal movement is possible, but there are no such restriction on sight. Each map tile has a movement cost; each unit has a maximal movement and sight value, and minimal to maximal attack range. Enemies and terrain can block movement, terrain can block sight, etc.

For pathfinding, <= 0 is NULL/impassable/blocked, and any positive value evaluates to true/passable/unblocked.

For convenience sake, this module also defines some useful structures: n-dim nmath_point_type, nmath_sq_neighbors_type (each square on a square 2D grid has 4 neighbors), nmath_hex_neighbors_type (each hexagon on a hexagonal 2D grid has 6 neighbors).

Definitions:

  • A costmap is a 2D matrix with tile traversal costs for a certain character/class.
  • A movemap is a 2D matrix with tiles a certain unit can move to.
  • A blockmap is a 2D matrix with tiles blocked for vision.
  • A sightmap is a 2D matrix with tiles visible by a certain unit.
  • Units can attack enemies that are on attackto tiles,
  • Units can attack enemies from attackfrom tiles,
  • Units can be push/pulled on pushto/pullto tiles,
  • Units can be only be push/pulled in pushto/pullto directions.

Some other useful function include the computation of a "unit gradient" that computes the distance to the closest unit on every tile, computing a path on a matrix using a list of points, etc. Some functions that are applicable to points on a hexagonal grid are also implemented.

Also, some functions can output arrays a list of points, or as a 2D matrix of same size to the input matrix. The list of points have unknown length below the matrix total length, so are created using DARR to have an accessible len value.

To Do

  • Vectorization (gcc only?)
  • Benchmarks
  • Determinant (requires LU decomposition) ?
  • Diagonalization ?
  • Eigenvalue/Eigenvector ?
  • Matrix Multiplication
  • Make operations less naive

Credits

Copyright (c) 2021 Average Bear Games, Made by Gabriel Taillon

noursmath's People

Contributors

gabinou avatar

Stargazers

 avatar

Watchers

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