npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gebf

v2.0.0

Published

This node.js module is created to study potential behind grammar evolution.

Downloads

3

Readme

gebf

This node.js module is created to study potential behind grammar evolution.

In a nutshell, it is a brute-force generator with predefined grammar that produces expressions using javascript test function.

Installation

npm install gebf

Usage

Define a grammar:

var lang = {...};

One need a test function, points and results as follows:

var testFunc = x => x + x;
var testPoints = [1, 2, ..., N];
var testResults = testPoints.map(x => testFunc(x));

And call the method:

var gebf = require('gebf');
var result = gebf(lang, testFuncc, testPoints, testResults);

Note that at this point gebf supports only one-argument test functions.

How to define a grammar properly

Grammar definition

A text wrapped with < and > is considered as a token and terminal otherwise.

Tokens are expandable grammar expressions and terminals are not.

Terminals can be binded to actual javascript functions with bindings key.

var lang = {
	entry: '<entry>', // always must be defined
	'<entry>': ['<sub1>', ... ,'<subN>'], // token and its sub-tokens
	'<sub1>' : [...],
	...
	'<subN>' : [...],
	bindings: {
		sin: Math.sin,
		cos: Math.cos,
	}
};

Example

var lang = {
	entry: '<expr>',
	'<expr>: [
		'<expr> <op> <expr>',
		'x',
		'1'
	],
	'<op>': ['+', '-'],
};

Can produce infinite expressions as follows:

		1 + x
		x - 1
		x - 1 + x
		x + x + 1 + 1

and so on

Actual example

The following code defines grammar that produces expressions with the very same output for given test formula.

var lang = {
	entry: "<expr>",
	"<expr>": [
		"<expr> <op> <expr>",
		"<fn>(<expr>)",
		"<var>",
		"<const>"
	],
	"<op>": ["+", "-", "*", "/"],
	"<fn>": ["sin", "cos",],
	"<var>": ["x",],
	"<const>": ["1", "2"],
	bindings: {
		sin: Math.sin,
		cos: Math.cos,
	}
};

// we are going to prove that given grammar can produce a formula sin(2*x)
var testFunc = x => 2 * Math.sin(x) * Math.cos(x); 
var testPoints = [-1, -.9, -.8, -.76, -.72, -.68, -.64, -.4, -.2, 0, .2, .4, .63, .72, .81, .9, .93, .96, .99, 1];
var testResults = testPoints.map(x => testFunc(x));

var gebf = require('gebf');

var result = gebf(lang, testFunc, testPoints, testResults);

console.log(result);

After a while, it produces the following result:

[ 'sin(2 * x)', 'sin(x * 2)', 'sin(x + x)' ]

Notes

Behind the curtain the algorithm uses integer array to represent complete grammar expression.

By default the maximum length for expression is 10.

For simple grammar it is more than enough. But if it is not, just pass one more parameter to the call like this:

gebf(lang, testFunc, testPoints, testResults, N);

where N is max length of expression

Unfortunately, due to brute-force approach it is not recommended.