Skip to content

Commit 84a9914

Browse files
committed
Fix matrix of matrices
1 parent f7c10b1 commit 84a9914

3 files changed

Lines changed: 14 additions & 7 deletions

File tree

examples/browser/rocket_trajectory_optimization.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ <h1>Rocket trajectory optimization</h1>
151151
label: resultName.slice(7),
152152
data: sim.evaluate(
153153
'concat('
154-
+ `(${resultName}[:,4] - phi0) * r0 / rad / km,` // Surface distance from start (in km)
155-
+ `(${resultName}[:,1] - r0) / km` // Height above surface (in km)
154+
+ `(${resultName}[:,[4]] - phi0) * r0 / rad / km,` // Surface distance from start (in km)
155+
+ `(${resultName}[:,[1]] - r0) / km` // Height above surface (in km)
156156
+ ')'
157157
).toArray().map(([x, y]) => ({ x, y })),
158158
borderColor: i % 2 ? '#999' : '#dc3912',
@@ -171,9 +171,10 @@ <h1>Rocket trajectory optimization</h1>
171171
.toArray()
172172
.map(([v, t]) => ({ x: t.toNumber('s'), y: v.toNumber('m/s') }))
173173
}])
174+
console.log(sim.evaluate("result[:,[6]]"))
174175
createChart([{
175176
label: 'height (in km)',
176-
data: sim.evaluate("concat((result[:, 1] - r0), result[:, 6])")
177+
data: sim.evaluate("concat((result[:,[1]] - r0) / km, result[:,[6]])")
177178
.toArray()
178179
.map(([r, t]) => ({ x: t.toNumber('s'), y: r.toNumber('km') })),
179180
}])
@@ -185,7 +186,7 @@ <h1>Rocket trajectory optimization</h1>
185186
}])
186187
createChart([{
187188
label: 'acceleration (in m/s^2)',
188-
data: sim.evaluate("concat(diff(result[:, 2]) ./ diff(result[:, 6]), result[:end-1, 6])")
189+
data: sim.evaluate("concat(diff(result[:, [2]]) ./ diff(result[:, [6]]), result[:end-1, [6]])")
189190
.toArray()
190191
.map(([acc, t]) => ({ x: t.toNumber('s'), y: acc.toNumber('m/s^2') })),
191192
}])

src/type/matrix/DenseMatrix.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ export const createDenseMatrixClass = /* #__PURE__ */ factory(name, dependencies
573573
const result = me.create(undefined, me._datatype)
574574
result._size = me._size
575575
if (isUnary || fastCallback.isUnary) {
576-
result._data = iterateUnary(me._data)
576+
result._data = preprocess(iterateUnary(me._data))
577577
return result
578578
}
579579
if (maxDepth === 0) {
@@ -582,12 +582,12 @@ export const createDenseMatrixClass = /* #__PURE__ */ factory(name, dependencies
582582
for (let i = 0; i < inputData.length; i++) {
583583
data[i] = fastCallbackFn(inputData[i], [i], me)
584584
}
585-
result._data = data
585+
result._data = preprocess(data)
586586
return result
587587
}
588588

589589
const index = []
590-
result._data = iterate(me._data)
590+
result._data = preprocess(iterate(me._data))
591591
return result
592592

593593
function iterate (data, depth = 0) {

test/unit-tests/type/matrix/DenseMatrix.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,12 @@ describe('DenseMatrix', function () {
724724
assert.deepStrictEqual(m2.valueOf(), [2, 4, 6])
725725
})
726726

727+
it('should return a matrix (not a matrix of matrices) when the result of map is a matrix.', function () {
728+
const m = new DenseMatrix([[1, 2]])
729+
const m2 = m.map(function (value) { return new DenseMatrix([value, value]) })
730+
assert.deepStrictEqual(m2.valueOf(), [[[1, 1], [2, 2]]])
731+
})
732+
727733
it('should work on empty matrices', function () {
728734
const m = new DenseMatrix([])
729735
const m2 = m.map(function (value) { return value * 2 })

0 commit comments

Comments
 (0)