Skip to content

Commit

Permalink
💯 Add ::Sum and sum operator
Browse files Browse the repository at this point in the history
  • Loading branch information
mlajtos committed Aug 15, 2018
1 parent 7b37472 commit be02a2e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/components/Interpreter/modules/Reducers/Sum/doc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Sum

Sum returns the sum of the provided tensor

```L1
::Sum
sum-1: Sum 1 ; sum-1 = 1
sum-2: Sum [1 2 3] ; sum-2 = 6
sum-3: +[1 2 3] ; sum-3 = 6
sum-4: +[1 2, 3 4] ; sum-4 = 10
```
11 changes: 11 additions & 0 deletions src/components/Interpreter/modules/Reducers/Sum/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as tf from "@tensorflow/tfjs-core"
import { of } from "rxjs"

import Symbols from "../../../symbols"

import doc from "./doc.md"

export default {
[Symbols.call]: (tensor) => of(tf.sum(tensor)),
[Symbols.doc]: doc
}
3 changes: 2 additions & 1 deletion src/components/Interpreter/modules/Reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Min from "./Min"
import Max from "./Max"
import Mean from "./Mean"
import Sum from "./Sum"

export default {
Min, Max, Mean
Min, Max, Mean, Sum
}
7 changes: 6 additions & 1 deletion src/components/Interpreter/modules/TensorOperators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import * as tf from "@tensorflow/tfjs"
const binarize = (fn) => (({ a, b }) => fn(a, b))

export default {
"+": binarize(tf.add),
"+": ({a, b}) => {
if (a === undefined) {
return tf.sum(b)
}
return tf.add(a, b)
},
"-": ({a, b}) => {
if (a === undefined) {
return tf.neg(b)
Expand Down
1 change: 1 addition & 0 deletions src/components/Parser/grammar.ohm
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ L1 {
Addition
= (Addition ("+"|"-") Multiplication) -- binary
| Multiplication -- fallthrough
| "+" Multiplication -- sum
| "-" Multiplication -- negative
Multiplication
= (Multiplication ("*"|"×"|"/"|"÷"|"%"|"@"|"⊗") Exponentiation) -- binary
Expand Down
1 change: 1 addition & 0 deletions src/components/Parser/semantics.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ const semantics = {
}
},
Addition_negative: function(op, v) { return unaryOperation(op, v, this.source) },
Addition_sum: function(op, v) { return unaryOperation(op, v, this.source) },
Multiplication_reciprocal: function(op, v) { return unaryOperation(op, v, this.source) },
PrimitiveExpression_magic: function(op, v) { return unaryOperation(op, v, this.source) },
PrimitiveExpression_paren: function(_, v, __) { return v.eval() },
Expand Down

0 comments on commit be02a2e

Please sign in to comment.