Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type-inference Gauss elimination example #548

Merged
merged 1 commit into from
Aug 20, 2024

Conversation

sharkdp
Copy link
Owner

@sharkdp sharkdp commented Aug 20, 2024

Run it here

# A silly example to demonstrate that Numbats type inference engine can be
# used to solve systems of linear equations (with rational coefficients) by
# encoding them as type constraints.
#
# Example system of equations from [1]:
#
#      2x + y -  z = 8
#     -3x - y + 2z = -11
#     -2x + y + 2z = -3
#
# With solution x = 2, y = 3, z = -1.
#
# [1] https://en.wikipedia.org/wiki/Gaussian_elimination

unit a

fn f(x, y, z) =
  (x^2 y z^-1 == a^8) && (x^-3 y^-1 z^2 == a^-11) && (x^-2 y z^2 == a^-3)

# Note: The choice of '==' and '&&' above is somewhat arbitrary. Instead of
# '==', we could have used any other operation that introduces an equality
# constraint, like '+', for example. Instead of '&&' to introduce multiple
# constraints, we could have used '*', for example.

# The solution can be read off from the type of f:

type(f)

@sharkdp sharkdp force-pushed the add-gauss-elimination-example branch 2 times, most recently from a202325 to 3d35881 Compare August 20, 2024 19:53
@sharkdp sharkdp force-pushed the add-gauss-elimination-example branch from 3d35881 to a474024 Compare August 20, 2024 19:55
@sharkdp sharkdp merged commit afd3a57 into master Aug 20, 2024
15 checks passed
@sharkdp sharkdp deleted the add-gauss-elimination-example branch August 20, 2024 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant