Research for number folding algorithms.
The detailed analysis is here. (Chinese edition only)
- math-folding-react: Web UI to represent the algorithm by using React JS and Material UI.
- math-folding-wechat: A mobile UI working on WeiXin mini program (微信小程序).
- simple-utils: A set of simple utilities for internal usage.
$ npm installFor 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;| 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. | ||
| 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. | ||
Currently I only resolved First-Order Folding (FOF) and Second-Order Folding (SOF).
So far, this bundle includes two algorithms:
-
Recursive
An algorithm that is simple to implement.
-
Formula
A non-recursive algorithm, which needs somewhat a complicate way to understand, especially for SOF.
You need a mocha or karma command line utility, for this package, mocha@5.2.0, mocha-webpack@2.0.0-beta.0 and karma@3.1.4 are recommended.
Inline script is available:
$ npm testor
$ npm run karma