|
| 1 | +/*! |
| 2 | + Copyright 2018 Propel http://propel.site/. All rights reserved. |
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + you may not use this file except in compliance with the License. |
| 5 | + You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | + Unless required by applicable law or agreed to in writing, software |
| 10 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + See the License for the specific language governing permissions and |
| 13 | + limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +import { getOutputHandler, PlotData } from "./output_handler"; |
| 17 | +import { isTensor } from "./util"; |
| 18 | + |
| 19 | +export function plot(...args) { |
| 20 | + if (!getOutputHandler()) { |
| 21 | + console.warn("plot: no output handler"); |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + const xs = []; |
| 26 | + const ys = []; |
| 27 | + let state = "x"; |
| 28 | + for (let i = 0; i < args.length; i++) { |
| 29 | + const arg = args[i]; |
| 30 | + switch (state) { |
| 31 | + case "x": |
| 32 | + xs.push(arg); |
| 33 | + state = "y"; |
| 34 | + break; |
| 35 | + |
| 36 | + case "y": |
| 37 | + ys.push(arg); |
| 38 | + state = "x"; |
| 39 | + break; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + const nSeries = Math.min(xs.length, ys.length); |
| 44 | + const data: PlotData = []; |
| 45 | + for (let i = 0; i < nSeries; ++i) { |
| 46 | + // TODO line = $.stack([xs[i], ys[i]], 1) |
| 47 | + const xv = isTensor(xs[i]) ? xs[i].dataSync() : xs[i]; |
| 48 | + const yv = isTensor(ys[i]) ? ys[i].dataSync() : ys[i]; |
| 49 | + const nPoints = Math.min(xv.length, yv.length); |
| 50 | + const line = []; |
| 51 | + for (let j = 0; j < nPoints; ++j) { |
| 52 | + line.push({ x: xv[j], y: yv[j] }); |
| 53 | + } |
| 54 | + data.push(line); |
| 55 | + } |
| 56 | + |
| 57 | + getOutputHandler().plot(data); |
| 58 | +} |
| 59 | + |
| 60 | +export function linspace(start: number, stop: number, num: number): number[] { |
| 61 | + const d = (stop - start) / (num - 1); |
| 62 | + const values = new Array(num); |
| 63 | + for (let i = 0; i <= num - 1; ++i) { |
| 64 | + values[i] = start + i * d; |
| 65 | + } |
| 66 | + return values; |
| 67 | +} |
0 commit comments