Repository Public
1import Foundation2import SheetCore3import Statistics45/// Result of a one-variable data table: one row per input value, one column per output cell.6public struct OneVariableDataTable: Sendable, Hashable, TableReport {7 public var inputLabel: String8 public var outputLabels: [String]9 public var inputs: [Value]10 /// values[i][j] = output j when the input cell holds inputs[i].11 public var values: [[Value]]1213 /// Header row (input label, output labels) followed by one row per input value.14 public func table() -> [[Value]] {15 var rows: [[Value]] = [[.text(inputLabel)] + outputLabels.map { .text($0) }]16 for (i, v) in inputs.enumerated() { rows.append([v] + values[i]) }17 return rows18 }19}2021/// Result of a two-variable data table, laid out like Excel's: row-input values across the top,22/// column-input values down the side.23public struct TwoVariableDataTable: Sendable, Hashable, TableReport {24 public var rowInputLabel: String25 public var columnInputLabel: String26 public var outputLabel: String27 /// Values substituted into the row input cell (they run across the top of the table).28 public var rowInputs: [Value]29 /// Values substituted into the column input cell (they run down the first column).30 public var columnInputs: [Value]31 /// values[i][j] = output with the column input = columnInputs[i] and the row input = rowInputs[j].32 public var values: [[Value]]3334 public func table() -> [[Value]] {35 var rows: [[Value]] = [[.text(outputLabel)] + rowInputs]36 for (i, v) in columnInputs.enumerated() { rows.append([v] + values[i]) }37 return rows38 }39}4041/// Data tables: the model is recalculated once per input value (or pair of values) and the42/// outputs are returned as plain values; the workbook's inputs are left as they were.43public enum DataTable {44 public static func oneVariable(engine: CalcEngine, input: ModelCell, values: [Value], outputs: [ModelCell],45 isolation: WhatIfIsolation = .clone) throws -> OneVariableDataTable {46 let wb = engine.workbook47 for c in [input] + outputs { guard c.isValid(in: wb) else { throw WhatIfError.invalidCell(c.label(in: wb)) } }48 let grid = try ModelSandbox.run(on: engine, isolation: isolation) { box in49 try values.map { v -> [Value] in50 try box.set(input, v)51 return try outputs.map { try box.value($0) }52 }53 }54 return OneVariableDataTable(inputLabel: input.label(in: wb), outputLabels: outputs.map { $0.label(in: wb) }, inputs: values, values: grid)55 }5657 public static func oneVariable(engine: CalcEngine, input: ModelCell, values: [Double], outputs: [ModelCell],58 isolation: WhatIfIsolation = .clone) throws -> OneVariableDataTable {59 try oneVariable(engine: engine, input: input, values: values.map { Value.number($0) }, outputs: outputs, isolation: isolation)60 }6162 public static func twoVariable(engine: CalcEngine, rowInput: ModelCell, rowValues: [Value], columnInput: ModelCell, columnValues: [Value],63 output: ModelCell, isolation: WhatIfIsolation = .clone) throws -> TwoVariableDataTable {64 let wb = engine.workbook65 for c in [rowInput, columnInput, output] { guard c.isValid(in: wb) else { throw WhatIfError.invalidCell(c.label(in: wb)) } }66 guard rowInput != columnInput else { throw WhatIfError.invalidOptions("the row and column input cells must differ") }67 let grid = try ModelSandbox.run(on: engine, isolation: isolation) { box in68 try columnValues.map { cv -> [Value] in69 try box.set(columnInput, cv)70 return try rowValues.map { rv in71 try box.set(rowInput, rv)72 return try box.value(output)73 }74 }75 }76 return TwoVariableDataTable(rowInputLabel: rowInput.label(in: wb), columnInputLabel: columnInput.label(in: wb), outputLabel: output.label(in: wb),77 rowInputs: rowValues, columnInputs: columnValues, values: grid)78 }7980 public static func twoVariable(engine: CalcEngine, rowInput: ModelCell, rowValues: [Double], columnInput: ModelCell, columnValues: [Double],81 output: ModelCell, isolation: WhatIfIsolation = .clone) throws -> TwoVariableDataTable {82 try twoVariable(engine: engine, rowInput: rowInput, rowValues: rowValues.map { Value.number($0) }, columnInput: columnInput,83 columnValues: columnValues.map { Value.number($0) }, output: output, isolation: isolation)84 }85}86