Skip to content

jungleford/math-folding

Repository files navigation

math-folding: 对折序列问题(Number Folding Problem) npm (scoped)

Research for number folding algorithms.

The detailed analysis is here. (Chinese edition only)

Related Projects

Install

$ npm install

Usage

For ES6,

import {Constants, FOF, SOF} from '@jungleford/math-folding';

For ES5,

var Constants = require('@jungleford/math-folding').Constants;
var FOF = require('@jungleford/math-folding').FOF;
var SOF = require('@jungleford/math-folding').SOF;

API

First Ording Folding (FOF)

Method Parameters Description
Folding power (Required) The exponent of the number of the elements. Constructor.
original (Optional) Assign the initial sequence to the folding service. If omitted, use the sequence of natural numbers.
init forceReset (Optional) True if you want to reset all internal states when initiating. Returns the original sequence.
getPower Returns the power, i.e. the value of k.
getCount Returns the total count of numbers, i.e. the value of n.
compute algorithm (Optional) The algorithm this service will use. Currently only "recursive" and "formula" are available. If omitted, "recursive" will be used. Returns the final sequence (one-dimension array). The core process of FOF. This method will mutate the internal states. User must call this method first to get the results.
callback (Optional) A function that allows you to do something else when computation is done, and this folding instance will be passed into the callback as an argument.
positionOf x (Required) The original number from 1 to n, or the object value in the original array. Returns the position of an original number or object in the final sequence. User must call compute() first.
valueOf p (Required) The position in the final sequence. Returns the value on the given position. User must call compute() first.
getSteps Returns all steps in the process of computation, this is a three-dimension array. Only avaiable for "recursive" algorithm.

Second Ording Folding (SOF)

Method Parameters Description
Folding power (Required) The exponent of the number of the elements. Constructor.
original (Optional) Assign the initial squre matrix to the folding service. This argument can be a matrix (two-dimension) or a sequence (one-dimension array). If omitted, use the sequence of natural numbers.
isFlat (Optional) True if "original" is a sequence (one-dimension array).
init forceReset (Optional) True if you want to reset all internal states when initiating. Returns the original matrix.
getPower Returns the power, i.e. the value of k.
getCount Returns the total count of numbers, i.e. the value of n.
getRowCount Returns the rows count of the result matrix., i.e. the value of 2^k.
compute algorithm (Optional) The algorithm this service will use. Currently only "recursive" and "formula" are available. If omitted, "recursive" will be used. Returns the final sequence (two-dimension array), and you should get the first member (i.e. compute(alg)[0]) as the result. The core process of SOF. This method will mutate the internal states. User must call this method first to get the results.
callback (Optional) A function that allows you to do something else when computation is done, and this folding instance will be passed into the callback as an argument.
positionOf x (Required) The original number from 1 to n, or the object value in the original matrix. Returns the position of an original number or object in the final sequence. User must call compute() first.
valueOf p (Required) The position in the final sequence. Returns the value on the given position. User must call compute() first.
getSteps Returns all steps in the process of computation, this is a three-dimension array. Only avaiable for "recursive" algorithm.

Algorithms

Currently I only resolved First-Order Folding (FOF) and Second-Order Folding (SOF).

So far, this bundle includes two algorithms:

  1. Recursive

    An algorithm that is simple to implement.

  2. Formula

    A non-recursive algorithm, which needs somewhat a complicate way to understand, especially for SOF.

Testing

You need a mocha or karma command line utility, for this package, [email protected], [email protected] and [email protected] are recommended.

Inline script is available:

$ npm test

or

$ npm run karma