Skip to content

Commit

Permalink
Print one based in the parser (#3009)
Browse files Browse the repository at this point in the history
* broadcasting

* Simplified broadcasting

* Updated for broadcasting

* Changed to camel case

* Camel case and auto formating

* Added comments

* Skip if matrices have the same size

* Fixed issue with undefined variable

missing dot  in `A._size`

* Implemented broadcasting in all functions

* Added helper functions

* Added function to check for broadcasting rules

* Tests for broadcasted arithmetic

* Fixed issue with matrix the size of a vector

* Documented and updated broadcasting

* Included broadcast.test

* Included math to syntax when missing

* Added print transform and tests

* Simplify conditional

* Included regex in an util

---------

Co-authored-by: David Contreras <[email protected]>
Co-authored-by: Jos de Jong <[email protected]>
  • Loading branch information
3 people authored Sep 6, 2023
1 parent 08bf93b commit c35a801
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ Evan Miller <[email protected]>
Timur <[email protected]>
Ari Markowitz <[email protected]>
Jay Wang <[email protected]>
David Contreras <[email protected]
Jaeu Jeong <[email protected]>
cyavictor88 <[email protected]>
David Contreras <[email protected]>
Expand Down
2 changes: 1 addition & 1 deletion src/expression/embeddedDocs/function/utils/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const printDocs = {
'print("Lucy is $age years old", {age: 5})',
'print("The value of pi is $pi", {pi: pi}, 3)',
'print("Hello, $user.name!", {user: {name: "John"}})',
'print("Values: $0, $1, $2", [6, 9, 4])'
'print("Values: $1, $2, $3", [6, 9, 4])'
],
seealso: ['format']
}
28 changes: 28 additions & 0 deletions src/expression/transform/print.transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { createPrint } from '../../function/string/print.js'
import { factory } from '../../utils/factory.js'
import { printTemplate } from '../../utils/print.js'

const name = 'print'
const dependencies = ['typed', 'matrix', 'zeros', 'add']

export const createPrintTransform = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, zeros, add }) => {
const print = createPrint({ typed, matrix, zeros, add })
return typed(name, {
'string, Object | Array': function (template, values) { return print(_convertTemplateToZeroBasedIndex(template), values) },
'string, Object | Array, number | Object': function (template, values, options) { return print(_convertTemplateToZeroBasedIndex(template), values, options) }
})

function _convertTemplateToZeroBasedIndex (template) {
return template.replace(printTemplate, (x) => {
const parts = x.slice(1).split('.')
const result = parts.map(function (part) {
if (!isNaN(part) && part.length > 0) {
return parseInt(part) - 1
} else {
return part
}
})
return '$' + result.join('.')
})
}
}, { isTransformFunction: true })
1 change: 1 addition & 0 deletions src/factoriesAny.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,4 @@ export { createSumTransform } from './expression/transform/sum.transform.js'
export { createQuantileSeqTransform } from './expression/transform/quantileSeq.transform.js'
export { createCumSumTransform } from './expression/transform/cumsum.transform.js'
export { createVarianceTransform } from './expression/transform/variance.transform.js'
export { createPrintTransform } from './expression/transform/print.transform.js'
6 changes: 5 additions & 1 deletion src/function/string/print.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { format } from '../../utils/string.js'
import { isString } from '../../utils/is.js'
import { factory } from '../../utils/factory.js'
import { printTemplate } from '../../utils/print.js'

const name = 'print'
const dependencies = ['typed']
Expand Down Expand Up @@ -66,9 +67,12 @@ export const createPrint = /* #__PURE__ */ factory(name, dependencies, ({ typed
* @private
*/
function _print (template, values, options) {
return template.replace(/\$([\w.]+)/g, function (original, key) {
return template.replace(printTemplate, function (original, key) {
const keys = key.split('.')
let value = values[keys.shift()]
if (value !== undefined && value.isMatrix) {
value = value.toArray()
}
while (keys.length && value !== undefined) {
const k = keys.shift()
value = k ? value[k] : value + '.'
Expand Down
1 change: 1 addition & 0 deletions src/utils/print.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const printTemplate = /\$([\w.]+)/g
19 changes: 19 additions & 0 deletions test/unit-tests/function/string/print.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ describe('print', function () {
}), 'hello, first last!')
})

it('should interpolate values from a nested object in a template (mixed object/matrix template)', function () {
assert.strictEqual(math.print('hello$separator.0 $name.first $name.last!', {
name: {
first: 'first',
last: 'last'
},
separator: math.matrix([','])
}), 'hello, first last!')
})

it('should round interpolate values with provided precision (object template)', function () {
assert.strictEqual(math.print('pi=$pi', { pi: math.pi }, 3), 'pi=3.14')
})
Expand Down Expand Up @@ -104,4 +114,13 @@ describe('print', function () {
const expression = math.parse('print(template,values)')
assert.strictEqual(expression.toTex(), '\\mathrm{print}\\left( template, values\\right)')
})

it('should work one indexed in the parser for all previous combinations', function () {
assert.deepStrictEqual(math.evaluate("print('I like $food', {food:'pizza'})"), 'I like pizza')
assert.deepStrictEqual(math.evaluate("print('I like $1', ['pizza'])"), 'I like pizza')
assert.deepStrictEqual(math.evaluate("print('I like $food.1', {food:['pizza']})"), 'I like pizza')
assert.deepStrictEqual(math.evaluate("print('I like $1.food', [{food:'pizza'}])"), 'I like pizza')
assert.deepStrictEqual(math.evaluate("print('I like $food.1 and $food.2', {food:['pizza','tacos']})"), 'I like pizza and tacos')
assert.deepStrictEqual(math.evaluate("print('Values: $1, $2, $3', [6, 9, 4])"), 'Values: 6, 9, 4')
})
})

0 comments on commit c35a801

Please sign in to comment.