fermi/f2.go

226 lines
6.5 KiB
Go
Raw Normal View History

package main
import (
"bufio"
"errors"
"fmt"
2024-06-09 11:43:03 +00:00
"math"
"os"
2024-06-09 11:43:03 +00:00
"strconv"
"strings"
)
const NORMAL90CONFIDENCE = 1.6448536269514727
2024-06-09 13:14:44 +00:00
const general_err_msg = "Valid inputs: 2 || * 2 || / 2 || 2 20 || * 2 20 || / 2 20 || clean || =: var || op var || clean || help || debug || exit"
// Actually, I should look up how do do a) enums in go, b) union types
type Lognormal struct {
low float64
high float64
}
type Dist struct {
Type string
Lognormal Lognormal
Samples []float64
}
2024-06-09 11:43:03 +00:00
// Parse line into Distribution
func parseLineErr(err_msg string) (string, Dist, error) {
2024-06-09 12:00:30 +00:00
fmt.Println(general_err_msg)
fmt.Println(err_msg)
2024-06-09 11:43:03 +00:00
return "", Dist{}, errors.New(err_msg)
}
2024-06-09 12:48:53 +00:00
func parseLine(line string, vars map[string]Dist) (string, Dist, error) {
words := strings.Split(strings.TrimSpace(line), " ")
2024-06-09 11:43:03 +00:00
op := ""
var dist Dist
switch words[0] {
case "*":
op = "*"
2024-06-09 11:43:03 +00:00
words = words[1:]
case "/":
op = "/"
2024-06-09 11:43:03 +00:00
words = words[1:]
case "+":
2024-06-09 11:43:03 +00:00
return parseLineErr("+ operation not implemented yet")
case "-":
2024-06-09 11:43:03 +00:00
return parseLineErr("- operation not implemented yet")
default:
2024-06-09 11:43:03 +00:00
op = "*" // later, change the below to
}
switch len(words) {
2024-06-09 11:43:03 +00:00
case 0:
2024-06-09 12:00:30 +00:00
return parseLineErr("Operator must have operand; can't operate on nothing")
case 1:
2024-06-09 12:48:53 +00:00
var_word, var_word_exists := vars[words[0]]
2024-06-09 11:43:03 +00:00
single_float, err1 := strconv.ParseFloat(words[0], 64)
2024-06-09 12:48:53 +00:00
switch {
case var_word_exists:
dist = var_word
case err1 == nil:
dist = Dist{Type: "Lognormal", Lognormal: Lognormal{low: single_float, high: single_float}, Samples: nil}
case err1 != nil && !var_word_exists:
2024-06-09 13:14:44 +00:00
return parseLineErr("Trying to operate on a scalar, but scalar is neither a float nor an assigned variable")
}
2024-06-09 11:43:03 +00:00
case 2:
new_low, err1 := strconv.ParseFloat(words[0], 64)
new_high, err2 := strconv.ParseFloat(words[1], 64)
if err1 != nil || err2 != nil {
return parseLineErr("Trying to operate by a distribution, but distribution is not specified as two floats")
}
dist = Dist{Type: "Lognormal", Lognormal: Lognormal{low: new_low, high: new_high}, Samples: nil}
default:
return parseLineErr("Other input methods not implemented yet")
}
2024-06-09 11:43:03 +00:00
return op, dist, nil
}
// Join distributions
// Multiply lognormals
2024-06-09 12:00:30 +00:00
func multiplyLogDists(l1 Lognormal, l2 Lognormal) Lognormal {
logmean1 := (math.Log(l1.high) + math.Log(l1.low)) / 2.0
logstd1 := (math.Log(l1.high) - math.Log(l1.low)) / (2.0 * NORMAL90CONFIDENCE)
logmean2 := (math.Log(l2.high) + math.Log(l2.low)) / 2.0
logstd2 := (math.Log(l2.high) - math.Log(l2.low)) / (2.0 * NORMAL90CONFIDENCE)
logmean_product := logmean1 + logmean2
logstd_product := math.Sqrt(logstd1*logstd1 + logstd2*logstd2)
2024-06-09 12:00:30 +00:00
h := logstd_product * NORMAL90CONFIDENCE
loglow := logmean_product - h
loghigh := logmean_product + h
return Lognormal{low: math.Exp(loglow), high: math.Exp(loghigh)}
}
2024-06-09 10:59:47 +00:00
func joinDists(old_dist Dist, new_dist Dist, op string) (Dist, error) {
switch {
case old_dist.Type == "Lognormal" && new_dist.Type == "Lognormal" && op == "*":
2024-06-09 12:00:30 +00:00
return Dist{Type: "Lognormal", Lognormal: multiplyLogDists(old_dist.Lognormal, new_dist.Lognormal), Samples: nil}, nil
2024-06-09 12:08:19 +00:00
case old_dist.Type == "Lognormal" && new_dist.Type == "Lognormal" && op == "/":
tmp_dist := Lognormal{low: 1.0 / new_dist.Lognormal.high, high: 1.0 / new_dist.Lognormal.low}
return Dist{Type: "Lognormal", Lognormal: multiplyLogDists(old_dist.Lognormal, tmp_dist), Samples: nil}, nil
default:
2024-06-09 11:46:51 +00:00
fmt.Printf("For now, can't do anything besides multiplying lognormals\n")
}
2024-06-09 12:48:53 +00:00
return old_dist, errors.New("Can't combine distributions in this way")
}
2024-06-09 11:46:51 +00:00
/* Pretty print distributions */
func prettyPrintLognormal(low float64, high float64) {
// fmt.Printf("=> %.1f %.1f\n", low, high)
fmt.Printf("=> ")
switch {
case math.Abs(low) >= 1_000_000_000_000:
fmt.Printf("%.1fT", low/1_000_000_000_000)
case math.Abs(low) >= 1_000_000_000:
fmt.Printf("%.1fB", low/1_000_000_000)
case math.Abs(low) >= 1_000_000:
fmt.Printf("%.1fM", low/1_000_000)
case math.Abs(low) >= 1_000:
fmt.Printf("%.1fK", low/1_000)
case math.Abs(low) >= 1_000:
fmt.Printf("%.1fK", low/1_000)
default:
fmt.Printf("%.1f", low)
}
fmt.Printf(" ")
switch {
case math.Abs(high) >= 1_000_000_000_000:
fmt.Printf("%.1fT", high/1_000_000_000_000)
case math.Abs(high) >= 1_000_000_000:
fmt.Printf("%.1fB", high/1_000_000_000)
case math.Abs(high) >= 1_000_000:
fmt.Printf("%.1fM", high/1_000_000)
case math.Abs(high) >= 1_000:
fmt.Printf("%.1fK", high/1_000)
case math.Abs(high) >= 1_000:
fmt.Printf("%.1fK", high/1_000)
default:
fmt.Printf("%.1f", high)
}
fmt.Printf("\n")
// fmt.Printf("=> %.1f %.1f\n", low, high)
}
func prettyPrintDist(dist Dist) {
if dist.Type == "Lognormal" {
prettyPrintLognormal(dist.Lognormal.low, dist.Lognormal.high)
} else {
fmt.Printf("%v", dist)
}
}
/* Main event loop */
func main() {
reader := bufio.NewReader(os.Stdin)
2024-06-09 13:14:44 +00:00
init_dist := Dist{Type: "Lognormal", Lognormal: Lognormal{low: 1, high: 1}, Samples: nil} // Could also just be a scalar
old_dist := init_dist
2024-06-09 12:48:53 +00:00
vars := make(map[string]Dist)
// Could eventually be a more complex struct with:
// { Dist, VariableMaps, ConfigParams } or smth
EventForLoop:
for {
input, _ := reader.ReadString('\n')
if strings.TrimSpace(input) == "" {
continue EventForLoop
}
2024-06-09 12:21:01 +00:00
{
words := strings.Split(strings.TrimSpace(input), " ")
switch {
case words[0] == "exit" || words[0] == "e":
break EventForLoop
case words[0] == "help" || words[0] == "h":
fmt.Println(general_err_msg)
continue EventForLoop
case words[0] == "debug" || words[0] == "d":
2024-06-09 12:48:53 +00:00
fmt.Printf("Old dist: %v\n", old_dist)
fmt.Printf("Vars: %v\n", vars)
continue EventForLoop
case words[0] == "=:" && len(words) == 2:
2024-06-09 13:14:44 +00:00
vars[words[1]] = old_dist
fmt.Printf("%s ", words[1])
prettyPrintDist(old_dist)
continue EventForLoop
case words[0] == "." || words[0] == "clean" || words[0] == "c":
old_dist = init_dist
fmt.Println()
continue EventForLoop
case words[0] == "=." && len(words) == 2:
vars[words[1]] = old_dist
fmt.Printf("%s ", words[1])
prettyPrintDist(old_dist)
old_dist = init_dist
fmt.Println()
2024-06-09 12:21:01 +00:00
continue EventForLoop
// Other possible cases:
// Save to file
// Sample n samples
// Save stack to a variable?
2024-06-09 13:14:44 +00:00
// clean stack
2024-06-09 12:48:53 +00:00
// Define a function? No, too much of a nerdsnipea
2024-06-09 12:21:01 +00:00
}
}
2024-06-09 12:48:53 +00:00
op, new_dist, err := parseLine(input, vars)
if err != nil {
2024-06-09 12:21:01 +00:00
continue EventForLoop
}
2024-06-09 12:21:01 +00:00
joint_dist, err := joinDists(old_dist, new_dist, op)
if err != nil {
2024-06-09 12:21:01 +00:00
continue EventForLoop
}
old_dist = joint_dist
2024-06-09 11:46:51 +00:00
prettyPrintDist(old_dist)
}
}