Merge branch 'develop' into commutative-error

This commit is contained in:
Sam Nolan 2022-04-25 11:00:45 -04:00
commit fe493af0b9
6 changed files with 19 additions and 18 deletions

View File

@ -0,0 +1,2 @@
dist/
storybook-static

View File

@ -131,7 +131,7 @@ export interface SquiggleChartProps {
/** variables declared before this expression */ /** variables declared before this expression */
environment?: unknown; environment?: unknown;
/** When the environment changes */ /** When the environment changes */
onEnvChange?(env: unknown): void; onChange?(expr: squiggleExpression): void;
/** CSS width of the element */ /** CSS width of the element */
width?: number; width?: number;
height?: number; height?: number;
@ -141,8 +141,7 @@ export const SquiggleChart: React.FC<SquiggleChartProps> = ({
squiggleString = "", squiggleString = "",
sampleCount = 1000, sampleCount = 1000,
outputXYPoints = 1000, outputXYPoints = 1000,
environment = [], onChange = () => {},
onEnvChange = () => {},
height = 60, height = 60,
width = NaN, width = NaN,
}: SquiggleChartProps) => { }: SquiggleChartProps) => {
@ -158,8 +157,8 @@ export const SquiggleChart: React.FC<SquiggleChartProps> = ({
let expressionResult = run(squiggleString, samplingInputs); let expressionResult = run(squiggleString, samplingInputs);
let internal: JSX.Element; let internal: JSX.Element;
if (expressionResult.tag === "Ok") { if (expressionResult.tag === "Ok") {
onEnvChange(environment);
let expression = expressionResult.value; let expression = expressionResult.value;
onChange(expression);
internal = ( internal = (
<SquiggleItem expression={expression} width={_width} height={height} /> <SquiggleItem expression={expression} width={_width} height={height} />
); );

View File

@ -3,6 +3,7 @@ import * as ReactDOM from "react-dom";
import { SquiggleChart } from "./SquiggleChart"; import { SquiggleChart } from "./SquiggleChart";
import { CodeEditor } from "./CodeEditor"; import { CodeEditor } from "./CodeEditor";
import styled from "styled-components"; import styled from "styled-components";
import type { squiggleExpression } from "@quri/squiggle-lang";
export interface SquiggleEditorProps { export interface SquiggleEditorProps {
/** The input string for squiggle */ /** The input string for squiggle */
@ -22,7 +23,7 @@ export interface SquiggleEditorProps {
/** The environment, other variables that were already declared */ /** The environment, other variables that were already declared */
environment?: unknown; environment?: unknown;
/** when the environment changes. Used again for notebook magic*/ /** when the environment changes. Used again for notebook magic*/
onEnvChange?(env: unknown): void; onChange?(expr: squiggleExpression): void;
/** The width of the element */ /** The width of the element */
width: number; width: number;
} }
@ -43,7 +44,7 @@ export let SquiggleEditor: React.FC<SquiggleEditorProps> = ({
diagramStart, diagramStart,
diagramStop, diagramStop,
diagramCount, diagramCount,
onEnvChange, onChange,
environment, environment,
}: SquiggleEditorProps) => { }: SquiggleEditorProps) => {
let [expression, setExpression] = React.useState(initialSquiggleString); let [expression, setExpression] = React.useState(initialSquiggleString);
@ -69,7 +70,7 @@ export let SquiggleEditor: React.FC<SquiggleEditorProps> = ({
diagramStop={diagramStop} diagramStop={diagramStop}
diagramCount={diagramCount} diagramCount={diagramCount}
environment={environment} environment={environment}
onEnvChange={onEnvChange} onChange={onChange}
/> />
</div> </div>
); );
@ -80,7 +81,7 @@ export function renderSquiggleEditorToDom(props: SquiggleEditorProps) {
ReactDOM.render( ReactDOM.render(
<SquiggleEditor <SquiggleEditor
{...props} {...props}
onEnvChange={(env) => { onChange={(expr) => {
// Typescript complains on two levels here. // Typescript complains on two levels here.
// - Div elements don't have a value property // - Div elements don't have a value property
// - Even if it did (like it was an input element), it would have to // - Even if it did (like it was an input element), it would have to
@ -96,10 +97,10 @@ export function renderSquiggleEditorToDom(props: SquiggleEditorProps) {
// viewof env = cell('normal(0,1)') // viewof env = cell('normal(0,1)')
// to work // to work
// @ts-ignore // @ts-ignore
parent.value = env; parent.value = expr;
parent.dispatchEvent(new CustomEvent("input")); parent.dispatchEvent(new CustomEvent("input"));
if (props.onEnvChange) props.onEnvChange(env); if (props.onChange) props.onChange(expr);
}} }}
/>, />,
parent parent

View File

@ -20,6 +20,7 @@ describe("eval on distribution functions", () => {
}) })
describe("unaryMinus", () => { describe("unaryMinus", () => {
testEval("mean(-normal(5,2))", "Ok(-5)") testEval("mean(-normal(5,2))", "Ok(-5)")
testEval("-normal(5,2)", "Ok(Normal(-5,2))")
}) })
describe("to", () => { describe("to", () => {
testEval("5 to 2", "Error(Math Error: Low value must be less than high value.)") testEval("5 to 2", "Error(Math Error: Low value must be less than high value.)")

View File

@ -232,13 +232,12 @@ let combineShapesContinuousDiscrete = (
i, i,
( (
fn(continuousShape.xs[i], discreteShape.xs[j]), fn(continuousShape.xs[i], discreteShape.xs[j]),
continuousShape.ys[i] *. discreteShape.ys[j] /. discreteShape.xs[j], continuousShape.ys[i] *. discreteShape.ys[j] /. Js.Math.abs_float(discreteShape.xs[j]),
), ),
) |> ignore ) |> ignore
() ()
} }
Belt.Array.set(outXYShapes, j, dxyShape) |> ignore Belt.Array.set(outXYShapes, j, dxyShape) |> ignore
()
} }
} }
@ -246,12 +245,11 @@ let combineShapesContinuousDiscrete = (
|> E.A.fmap(XYShape.T.fromZippedArray) |> E.A.fmap(XYShape.T.fromZippedArray)
|> E.A.fold_left( |> E.A.fold_left(
(acc, x) => (acc, x) =>
XYShape.PointwiseCombination.combine( XYShape.PointwiseCombination.addCombine(
(a, b) => Ok(a +. b),
XYShape.XtoY.continuousInterpolator(#Linear, #UseZero), XYShape.XtoY.continuousInterpolator(#Linear, #UseZero),
acc, acc,
x, x,
)->E.R.toExn("Error, unexpected failure", _), ),
XYShape.T.empty, XYShape.T.empty,
) )
} }

View File

@ -52,7 +52,7 @@ module Normal = {
switch operation { switch operation {
| #Add => Some(#Normal({mean: n1 +. n2.mean, stdev: n2.stdev})) | #Add => Some(#Normal({mean: n1 +. n2.mean, stdev: n2.stdev}))
| #Subtract => Some(#Normal({mean: n1 -. n2.mean, stdev: n2.stdev})) | #Subtract => Some(#Normal({mean: n1 -. n2.mean, stdev: n2.stdev}))
| #Multiply => Some(#Normal({mean: n1 *. n2.mean, stdev: n1 *. n2.stdev})) | #Multiply => Some(#Normal({mean: n1 *. n2.mean, stdev: Js.Math.abs_float(n1) *. n2.stdev}))
| _ => None | _ => None
} }
@ -60,8 +60,8 @@ module Normal = {
switch operation { switch operation {
| #Add => Some(#Normal({mean: n1.mean +. n2, stdev: n1.stdev})) | #Add => Some(#Normal({mean: n1.mean +. n2, stdev: n1.stdev}))
| #Subtract => Some(#Normal({mean: n1.mean -. n2, stdev: n1.stdev})) | #Subtract => Some(#Normal({mean: n1.mean -. n2, stdev: n1.stdev}))
| #Multiply => Some(#Normal({mean: n1.mean *. n2, stdev: n1.stdev *. n2})) | #Multiply => Some(#Normal({mean: n1.mean *. n2, stdev: n1.stdev *. Js.Math.abs_float(n2)}))
| #Divide => Some(#Normal({mean: n1.mean /. n2, stdev: n1.stdev /. n2})) | #Divide => Some(#Normal({mean: n1.mean /. n2, stdev: n1.stdev /. Js.Math.abs_float(n2)}))
| _ => None | _ => None
} }
} }