Leedehai / typst-physics

physica: vectors, fields, differentials, derivatives, Dirac brakets, tensors, and more. See examples in the manual PDF.
https://github.com/Leedehai/typst-physics
MIT License
359 stars 11 forks source link

Abbreviation of evaluated conflicts with built-in function eval #22

Open linuxrider opened 10 months ago

linuxrider commented 10 months ago

The abbreviation of evaluated conflicts with typst built-in function eval.

Leedehai commented 10 months ago

I'd prefer not to rename physica's eval because

As a workaround, you may rename eval while importing if you would also want to keep access to Typst's eval at the same time.

#import "@preview/physica:0.9.2": .., eval as evalat

I understand users like doing wildcard imports (that is, import everything: #import "...": *), so I proposed https://github.com/typst/typst/issues/3068 to optionally hide symbols after discovering they'd like to use some symbols eclipsed by the import (instead of having to delete the * and write out every symbol they have ever used in the doc -- very bad UX), like the Dart language. But that proposal was "not liked at all" and my reasons were deemed "completely unfounded", so it was closed.

Maybe give a bump to Typst's https://github.com/typst/typst/issues/1639, which proposes another way to access the native Typst symbols that are otherwise eclipsed by imports :)

linuxrider commented 10 months ago

I understand. Is this naming conflict documented somewhere?

This is my use case of typst's eval.

#import "@preview/cetz:0.2.0": canvas, plot, draw

#let E-hermitepsi = ((0, "1/(calc.sqrt(calc.sqrt(calc.pi)))*(1 * f)"), (1, "1/(calc.sqrt(calc.sqrt(4*calc.pi)))*(2 * y * f)"), (2, "1/(calc.sqrt(calc.sqrt(64*calc.pi)))*(4 * y * y - 2) * f"), (3, "1/(calc.sqrt(calc.sqrt(2304*calc.pi)))*(8 * y * y * y - 12 * y)*f"))
#figure(
  grid(
    columns: 2,     // 2 means 2 auto-sized columns
    gutter: 2mm,    // space between columns
    canvas(length: 1.2cm, {
      import draw: *
      plot.plot(
        size: (5, 10),
        x-tick-step: 2,
        x-label: [$y$],
        // x-ticks: ( (0, $0$), (1, $a$)),
        y-label: [$V(x)$, #text([$E$], fill:blue), #text([$psi(y)$], fill:red)],
        y-tick-step: none,
        y-ticks: ((0.5, $v=0$), (1.5, $v=1$), (2.5, $v=2$), (3.5, $v=3$)),
        x-min: -4,
        x-max: 4,
        y-min: 0,
        // y-max: 12,
        {  
          plot.add(style: (stroke: (paint:black)),
            domain: (-3, 3), samples: 100, y => 0.5*y*y
          )
          for (v, hermitepsi) in E-hermitepsi {
            let E = v + 1/2
            let psi(y) = eval(hermitepsi, scope: (y:y, f: calc.exp(-y*y/2)))
              plot.add(style: (stroke: (paint:blue)),
                domain: (-4, 4), samples: 10, y => E
              )
              plot.add(style: (stroke: (paint:red)),
               domain: (-4, 4), samples: 100, y => psi(y) + E
              )
          }
        }
      )
      line(style: (stroke: (thickness: 5pt)), (2.5, 0), (2.5, 10))  
    }),
    canvas(length: 1.2cm, {
      import draw: *
      plot.plot(
        size: (5, 10),
        x-tick-step: 2,
        x-label: [$y$],
        y-label: [$V(x)$, #text([$E$], fill:blue), #text([$psi^2(y)$], fill:red)],
        y-tick-step: none,
        y-ticks: ( (0.5, $v=0$), (1.5, $v=1$),(2.5, $v=2$),(3.5, $v=3$)),
        x-min: -4,
        x-max: 4,
        y-min: 0,
        // y-max: 12,
        {  
          plot.add(style: (stroke: (paint:black)),
            domain: (-3, 3), samples: 100, y => 0.5*y*y
          )
          for (v, hermitepsi) in E-hermitepsi {
            let E = v + 1/2
            let psi(y) = eval(hermitepsi, scope: (y:y, f: calc.exp(-y*y/2), E: E))
            plot.add(style: (stroke: (paint:blue)),
              domain: (-4, 4), samples: 10, y => E
            )
            plot.add(style: (stroke: (paint:red)),
             domain: (-4, 4), samples: 100, y => psi(y) * psi(y) +  E
            )
          }
        }
      )
      line(style: (stroke: (thickness: 5pt)), (2.5, 0), (2.5, 10))
    })
  )
)

harmonic_oscillator