GithubHelp home page GithubHelp logo

fast_eca's Introduction

Fast and minimal C ECA implementation

Methods

The following methods are available in the header "fast_eca/wolfram_automaton.h":

  • autom_t* create_automat(size_t size, int states): This creates a pointer to an autom_t structure, representing an automaton of size size with states states.
  • int init_automat(autom_t* autom, enum InitMode mode): Initializes the automaton with:
    1. One cell in state 1 in the middle of the grid and the rest to 0 if mode == ONE
    2. Random states if mode == RANDOM
  • uint8_t* rule_from_number(unsigned long rule_number, int states, int neighbors): Creates an ECA rule (array of uint8_t) from its number, number of states and neighborhood size.
  • int update_step(autom_t* autom, uint8_t* rule, int radius): Updates the automaton one step at a time with the rule and radius (radius is neighbords/2 - 1).
  • int print_autom(autom_t* autom, char* buf): Format the automaton to a buffer. The buffer needs to be allocated to at least the size of (size + 1) char.

Try it out

Clone this folder in your project.

git clone https://github.com/hugcis/fast_eca.git

Put this C code into a file main.c, next to the folder fast_eca:

#include <stdio.h>
#include <inttypes.h>
#include "fast_eca/wolfram_automaton.h"

int main(int argc, char** argv) {
  size_t size = atoi(argv[1]);
  int neighbors = 3;
  int radius = (neighbors - 1) / 2;
  int states = 2;

  uint8_t* rule = rule_from_number(atoi(argv[2]), states, neighbors);
  autom_t* autom = create_automat(size, states);
  char* buf = calloc(size + 1, sizeof(char));
  init_automat(autom, ONE);

  for (int i = 0; i < 100; ++i) {
    update_step(autom, rule, radius);
    print_autom(autom, buf);
    fprintf(stdout, "%s\n", buf);
  }
}

Compile with the following command:

gcc -O3 -o main main.c -I. fast_eca/wolfram_automaton.c

And run (Grid size 100, rule 110)

./main 100 110

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.