Skip to content

Commit

Permalink
Support for array of equations
Browse files Browse the repository at this point in the history
Usage is for defining a system of equations and extracting the coefficient matrix and constant vector.
  • Loading branch information
ja72 committed Oct 29, 2021
1 parent fde1952 commit 386d0c3
Show file tree
Hide file tree
Showing 8 changed files with 860 additions and 117 deletions.
282 changes: 233 additions & 49 deletions Expressions/Expr.cs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Expressions/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public Function PartialDerivative(string variable)
return new Function($"{Name}_{variable}", body, Parameters);
}

public Expr Jacobian() => Body.Jacobian(Parameters);

public IQuantity NewtonRaphson(IQuantity init, double target = 0, double tolerance = 1e-11, int maxIter = 100)
{
if (init.IsScalar && Parameters.Length != 1)
Expand Down
54 changes: 50 additions & 4 deletions Expressions/Parsing/ExprParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public ExprParser(Tokenizer tokenizer)
public Expr ParseExpression()
{
// For the moment, all we understand is add and subtract
var expr = ParseEquals();
var expr = ParseArray();

// Check everything was consumed
if (tokenizer.Current.Token != Token.EOF)
Expand All @@ -30,8 +30,52 @@ public Expr ParseExpression()
return expr;
}

// TODO: Parse assignments before add/subtract
Expr ParseEquals()
// TODO: Parse arrays before assignment

Expr ParseArray()
{
var token = tokenizer.Current.Token;
switch (token)
{
case Token.OpenBracket:
// Skip '['
tokenizer.MoveNext();

// Parse arguments
var arguments = new List<Expr>();
while (true)
{
var lhs = ParseAssignment();
// Parse argument and add to list
// TODO: Parse arrays as elements first
arguments.Add(lhs);

// Is there another argument?
if (tokenizer.Current.Token == Token.Comma)
{
tokenizer.MoveNext();
continue;
}

// Get out
break;
}

// Check and skip ')'
if (tokenizer.Current.Token != Token.CloseBracket)
throw new SyntaxException("Missing close bracket");
tokenizer.MoveNext();

// Create the vector expression
if (arguments.Count==0) return Expr.Array(Array.Empty<Expr>());
// INFO: Do I want to cast an array of one as a scalar?
// if (arguments.Count==1) return arguments[0];
return Expr.Array(arguments);
}
return ParseAssignment();
}

Expr ParseAssignment()
{
var lhs = ParseAddSubtract();

Expand Down Expand Up @@ -198,6 +242,7 @@ Expr ParseLeaf()
node = Expr.Const(tokenizer.Current.Number);
tokenizer.MoveNext();
return node;

case Token.OpenBracket:
// Skip '['
tokenizer.MoveNext();
Expand All @@ -207,7 +252,8 @@ Expr ParseLeaf()
while (true)
{
// Parse argument and add to list
arguments.Add(ParseAddSubtract());
// TODO: Parse arrays as elements first
arguments.Add(ParseAssignment());

// Is there another argument?
if (tokenizer.Current.Token == Token.Comma)
Expand Down
Loading

0 comments on commit 386d0c3

Please sign in to comment.