Skip to content

Commit

Permalink
Add tslint
Browse files Browse the repository at this point in the history
  • Loading branch information
Maarten committed Jan 8, 2017
1 parent 52d0705 commit e61a134
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 123 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "semiring-src",
"version": "1.2.2",
"version": "1.2.3",
"main": "index.js",
"types": "index.d.ts",
"repository": "git@github.com:digitalheir/semiring-js.git",
Expand Down
4 changes: 2 additions & 2 deletions src/abstract-expression/atom.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Expression} from "./expression";

function isNumber(x:any): x is number {
return typeof x === 'number';
function isNumber(x: any): x is number {
return typeof x === "number";
}

export class Atom<T> implements Expression<T> {
Expand Down
2 changes: 1 addition & 1 deletion src/abstract-expression/atom/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Atom} from "./atom";

export class Num extends Atom<number> {
toString(radix?: number): string {
return "{"+this.value.toString(radix)+"}";
return "{" + this.value.toString(radix) + "}";
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/expression/binary-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ export abstract class BinaryFunction<T> implements Expression<T> {
}

export class WrappedBinaryFunction<T> extends BinaryFunction<T> {
private f: (x: T, y: T)=>T;
private f: (x: T, y: T) => T;

constructor(left: Expression<T>, right: Expression<T>, f: (x: T, y: T)=>T) {
constructor(left: Expression<T>, right: Expression<T>, f: (x: T, y: T) => T) {
super(left, right);
this.f = f;
}
Expand All @@ -33,7 +33,7 @@ export class WrappedBinaryFunction<T> extends BinaryFunction<T> {
};
}

export function wrapBinaryFunction<T>(left: Expression<T>, right: Expression<T>, f: (x: T, y: T)=>T): WrappedBinaryFunction<T> {
export function wrapBinaryFunction<T>(left: Expression<T>, right: Expression<T>, f: (x: T, y: T) => T): WrappedBinaryFunction<T> {
return new WrappedBinaryFunction(left, right, f);
}

Expand Down
102 changes: 98 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,104 @@ import {LogSemiring as ls, fromProbability, toProbability} from "./semirings/log
import {BooleanSemiring as bs} from "./semirings/boolean";
import {TropicalSemiring as ts} from "./semirings/tropical";
import {FloatingPointSemiring as fs} from "./semirings/floating-point";
import {Semiring, Property} from "./semiring";

/**
* Semiring defined on objects of type T.
*
* A semiring is specified by two binary operations ⊕ and ⊗ and two designated elements ZERO and ONE with the following properties:
*
* - ⊕: associative, commutative, and has 0 as its identity.
* - ⊗: associative and has identity 1, distributes w.r.t. ⊕, and has 0 as an annihilator: 0 ⊗ a = a ⊗ 0 = 0.
*
* A semiring can have algebraic properties (commutative, idempotent, etc). These need to be passed explicitly and are
* not checked for validity. So take care if you rely on these properties.
*
*/
export interface Semiring<T> {
// properties: {[prop: string]: boolean};
/**
* Also known as 'one', because 'one' ⊗ x = x
*/
multiplicativeIdentity: T;
/**
* Also known as 'zero', because 'zero' ⊕ x = x
*/
additiveIdentity: T;

/**
* ⊕
*/
plus: (x: T, y: T) => T;

/**
* ⊗
*/
times: (x: T, y: T) => T;

// /**
// *
// * @param properties
// */
// constructor(properties: Property[]|{[prop: string]: boolean}) {
// if (isPropArray(properties)) {
// this.properties = {};
// for (let prop of properties) {
// let property: string = Property[prop];
// this.properties[property] = true;
// }
// } else
// this.properties = properties;
// }
//
// /**
// *
// * @returns properties dictionary
// */
// public getProperties(): {[prop: string]: boolean} {
// return this.properties;
// };
//
// /**
// *
// * @param prop
// * @returns {boolean} Whether this semiring has given property
// */
// public hasProperty(prop: Property): boolean {
// const propertyName: string = Property[prop];
// let notHasProperty = !this.properties[propertyName];
// return !notHasProperty;
// };
}

export default Semiring;

export enum Property {
/**
* If times right-distributes wrt plus
*/
RightSemiring,
/**
* If times left-distributes wrt plus
*/
LeftSemiring,
/**
* An idempotent semiring is one whose addition is idempotent: a + a = a, for all a
*/
Idempotent,
/**
* A commutative semiring is one whose multiplication is commutative.
*/
Commutative,
/**
* ∀ a, b: a ⊕ b = a or a ⊕ b = b
*/
Path
}


//noinspection JSUnusedLocalSymbols
function isPropArray(x: Property[]|{[p: string]: boolean}): x is Property[] {
return Object.prototype.toString.call(x) === '[object Array]';
return Object.prototype.toString.call(x) === "[object Array]";
}

/**
Expand All @@ -27,7 +120,7 @@ export function makeDeferrable<T>(semiring: Semiring<T>): Semiring<Expression<T>

plus: (left, right) => wrapBinaryFunction(left, right, semiring.plus),
times: (left, right) => wrapBinaryFunction(left, right, semiring.times)
}
};
}

export const LogSemiring: Semiring<number> = ls;
Expand All @@ -41,4 +134,5 @@ export const Num = NumClass;
export const fromProbabilityToMinusLog: (x: number) => number = fromProbability;
export const toProbabilityFromMinusLog: (x: number) => number = toProbability;

export const Atom = AtomClass;
export const Atom = AtomClass;

92 changes: 0 additions & 92 deletions src/semiring.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/semirings/boolean.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Bool} from "../abstract-expression/atom/boolean";
import {Expression} from "../abstract-expression/expression";
import {Disjunction, Conjunction} from "../abstract-expression/boolean";
import {Semiring} from "../semiring";
import {Semiring} from "../index";

export function OR(x: boolean, y: boolean) {
return x || y;
Expand Down
2 changes: 1 addition & 1 deletion src/semirings/floating-point.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Expression} from "../abstract-expression/expression";
import {Multiplication, Addition} from "../abstract-expression/arithmetic";
import {Num} from "../abstract-expression/atom/number";
import {Semiring} from "../semiring";
import {Semiring} from "../index";

export function ADD(x: number, y: number): number {
// console.log(x+" + "+y + " = "+(x+y));
Expand Down
12 changes: 6 additions & 6 deletions src/semirings/log.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {MULTIPLY, ADD} from "./floating-point";
import {Semiring} from "../semiring";
import {ADD} from "./floating-point";
import {Semiring} from "../index";

export const LogSemiring: Semiring<number> = {
// constructor() {
Expand All @@ -11,7 +11,7 @@ export const LogSemiring: Semiring<number> = {
multiplicativeIdentity: 0.0,


plus: (x:number,y:number)=>{
plus: (x: number, y: number) => {
if (x === Infinity)
return y;
else if (y === Infinity)
Expand All @@ -23,13 +23,13 @@ export const LogSemiring: Semiring<number> = {
};

export function fromProbability(x: number): number {
if (x > 1.0 || x < 0.0) throw new Error("Can't have probabilities >1.0 or <0.0: "+x);
if (x > 1.0 || x < 0.0) throw new Error("Can't have probabilities >1.0 or <0.0: " + x);
return -Math.log(x);
}

export function toProbability(x: number): number {
let p = Math.exp(-x);
if (p > 1.0 || p < 0.0) throw new Error("Can't have probabilities >1.0 or <0.0: "+x);
const p = Math.exp(-x);
if (p > 1.0 || p < 0.0) throw new Error("Can't have probabilities >1.0 or <0.0: " + x);
return p;
}

Expand Down
8 changes: 4 additions & 4 deletions src/semirings/tropical.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {MULTIPLY, ADD} from "./floating-point";
import {Semiring} from "../semiring";
import {ADD} from "./floating-point";
import {Semiring} from "../index";

/**
* A tropical semiring defined with `min` as semiring addition
Expand All @@ -8,8 +8,8 @@ export const TropicalSemiring: Semiring<number> = {
additiveIdentity: Infinity,
multiplicativeIdentity: 0.0,

plus: (x:number,y:number)=>{
return Math.min(x,y);
plus: (x: number, y: number) => {
return Math.min(x, y);
},
times: ADD
};
Expand Down
41 changes: 41 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"rules": {
"class-name": true,
"comment-format": [true,
"check-space"
],
"indent": [true,
"spaces"
],
"one-line": [true,
"check-open-brace",
"check-whitespace"
],
"no-var-keyword": true,
"quotemark": [true,
"double",
"avoid-escape"
],
"semicolon": true,
"whitespace": [true,
"check-branch",
"check-decl",
"check-operator",
"check-module",
"check-separator",
"check-type"
],
"typedef-whitespace": [true, {
"call-signature": "nospace",
"index-signature": "nospace",
"parameter": "nospace",
"property-declaration": "nospace",
"variable-declaration": "nospace"
}],
"no-internal-module": true,
"no-trailing-whitespace": true,
"no-inferrable-types": true,
"no-null-keyword": true,
"prefer-const": true
}
}
2 changes: 1 addition & 1 deletion version.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
exports.default = "1.2.2";
exports.default = "1.2.3";
Loading

0 comments on commit e61a134

Please sign in to comment.