Skip to content

Commit

Permalink
Merge pull request #25 from metaindu/interrogation-unknown
Browse files Browse the repository at this point in the history
Interrogation unknown
  • Loading branch information
izrik committed Nov 12, 2021
2 parents 324df7d + cc56d6b commit 226e9fa
Show file tree
Hide file tree
Showing 37 changed files with 352 additions and 394 deletions.
2 changes: 1 addition & 1 deletion Compiler/ILCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public IEnumerable<Instruction> ConvertToInstructions(
public IEnumerable<Instruction> ConvertToInstructions(
Literal expr, VariableToArgumentNumberMapper varmap)
{
if (expr.Value.IsScalar(null))
if (expr.Value.IsIsScalar(null))
return new []
{
Instruction.LoadConstant(expr.Value.ToFloat())
Expand Down
30 changes: 1 addition & 29 deletions Expressions/ColorExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,34 +105,6 @@ public override void AcceptVisitor(IExpressionVisitor visitor)
throw new NotImplementedException();
}

public override IMathObject Result { get; } = new ResultC();
private class ResultC : IMathObject
{
public bool IsScalar(SolusEnvironment env) => true;
public bool IsVector(SolusEnvironment env) => false;
public bool IsMatrix(SolusEnvironment env) => false;
public int GetTensorRank(SolusEnvironment env) => 0;
public bool IsString(SolusEnvironment env) => false;

public int GetDimension(SolusEnvironment env, int index)
{
throw new IndexOutOfRangeException(
"Color expressions do not have dimensions");
}

public int[] GetDimensions(SolusEnvironment env)
{
throw new IndexOutOfRangeException(
"Color expressions do not have dimensions");
}

public int GetVectorLength(SolusEnvironment env)
{
throw new InvalidOperationException(
"Color expressions do not have a length");
}

public bool IsConcrete => false;
}
public override IMathObject Result => ScalarMathObject.Value;
}
}
114 changes: 34 additions & 80 deletions Expressions/ComponentAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ private IMathObject[] GetEvaledIndexes(SolusEnvironment env)
}

private static void CheckIndexes(IMathObject[] indexes,
bool exprIsScalar, bool exprIsVector, bool exprIsMatrix,
int exprTensorRank, bool exprIsString, int? exprLength,
bool? exprIsScalar, bool? exprIsVector, bool? exprIsMatrix,
int? exprTensorRank, bool? exprIsString, int? exprLength,
int? exprRowCount, int? exprColumnCount)
{
if (exprIsString)
if (exprIsString.HasValue && exprIsString.Value)
{
if (1 != indexes.Length)
throw new IndexException(
Expand All @@ -72,7 +72,8 @@ private static void CheckIndexes(IMathObject[] indexes,
}
else
{
if (exprIsScalar || exprTensorRank < 1)
if ((exprIsScalar.HasValue && exprIsScalar.Value) ||
exprTensorRank < 1)
throw new OperandException(
"Scalars do not have components");
if (exprTensorRank != indexes.Length)
Expand All @@ -83,23 +84,23 @@ private static void CheckIndexes(IMathObject[] indexes,

// TODO: maybe pass the env, and then we don't have to eval the
// indexes before-hand?
if (indexes.Any(i => !i.IsScalar(null)))
if (indexes.Any(i => !i.IsIsScalar(null)))
throw new IndexException(
"Indexes must be scalar");
if (indexes.Any(i => i.ToNumber().Value < 0))
throw new IndexException(
"Indexes must not be negative");

var index0 = (int) indexes[0].ToNumber().Value;
if (exprIsVector)
if (exprIsVector.HasValue && exprIsVector.Value)
{
if (index0 >= exprLength.Value)
throw new IndexException(
"Index exceeds the size of the vector");
return;
}

if (exprIsString)
if (exprIsString.HasValue && exprIsString.Value)
{
// TODO: check index for string
// if (!exprLength.HasValue)
Expand All @@ -112,7 +113,7 @@ private static void CheckIndexes(IMathObject[] indexes,
}

var index1 = (int) indexes[1].ToNumber().Value;
if (exprIsMatrix)
if (exprIsMatrix.HasValue && exprIsMatrix.Value)
{
if (index0 >= exprRowCount)
throw new IndexException(
Expand All @@ -132,35 +133,32 @@ public static IMathObject AccessComponent(IMathObject expr,
IMathObject[] indexes, SolusEnvironment env)
{
int? length = null;
if (expr.IsVector(env)) length = expr.ToVector().Length;
else if (expr.IsString(env)) length = expr.ToStringValue().Length;
int? exprRowCount = null;
int? exprColumnCount = null;
if (expr.GetTensorRank(env) > 1)
{
exprRowCount = new int?(expr.GetDimension(env, 0));
exprColumnCount = new int?(expr.GetDimension(env, 1));
}
if (expr.IsIsVector(env))
length = expr.ToVector().Length;
else if (expr.IsIsString(env))
length = expr.ToStringValue().Length;
var exprRowCount = expr.GetDimension(env, 0);
var exprColumnCount = expr.GetDimension(env, 1);
CheckIndexes(indexes, expr.IsScalar(env), expr.IsVector(env),
expr.IsMatrix(env), expr.GetTensorRank(env),
expr.IsString(env), length,
exprRowCount, exprColumnCount);

var index0 = (int) indexes[0].ToNumber().Value;
if (expr.IsVector(env))
if (expr.IsIsVector(env))
{
var v = expr.ToVector();
return v[index0];
}

if (expr.IsString(env))
if (expr.IsIsString(env))
{
var sv = expr.ToStringValue();
return sv.Value[index0].ToStringValue();
}

var index1 = (int) indexes[1].ToNumber().Value;
if (expr.IsMatrix(env))
if (expr.IsIsMatrix(env))
{
var m = expr.ToMatrix();
return m[index0, index1];
Expand All @@ -175,13 +173,13 @@ public static Expression AccessComponent(Expression expr,
IMathObject[] indexes, SolusEnvironment env)
{
int? length = null;
if (expr.Result.IsVector(env))
if (expr.Result.IsIsVector(env))
length = expr.Result.GetVectorLength(env);

int exprTensorRank = expr.Result.GetTensorRank(env);
int? exprTensorRank = expr.Result.GetTensorRank(env);
int? exprRowCount = null;
int? exprColumnCount = null;
if (exprTensorRank == 2)
if (exprTensorRank.HasValue && exprTensorRank.Value == 2)
{
exprRowCount = expr.Result.GetDimension(env, 0);
exprColumnCount = expr.Result.GetDimension(env, 1);
Expand Down Expand Up @@ -235,7 +233,7 @@ public override Expression Clone()
public override void AcceptVisitor(IExpressionVisitor visitor)
{
visitor.Visit(this);

Expr.AcceptVisitor(visitor);
foreach (var index in Indexes)
index.AcceptVisitor(visitor);
Expand All @@ -259,64 +257,20 @@ public override string ToString()

private class ResultC : IMathObject
{
// TODO: really, we can only interrogate the component if the
// object is known to have components all of a particular type,
// e.g. 3-vector in R^3. otherwise, we have to evaluate the
// indexes.
public ResultC(ComponentAccess ca) => _ca = ca;
private readonly ComponentAccess _ca;
public bool IsScalar(SolusEnvironment env)
{
var evaledIndexes = _ca.GetEvaledIndexes(env);
var expr = AccessComponent(_ca.Expr, evaledIndexes, env);
return expr.Result.IsScalar(env);
}

public bool IsVector(SolusEnvironment env)
{
var evaledIndexes = _ca.GetEvaledIndexes(env);
var expr = AccessComponent(_ca.Expr, evaledIndexes, env);
return expr.Result.IsVector(env);
}

public bool IsMatrix(SolusEnvironment env)
{
var evaledIndexes = _ca.GetEvaledIndexes(env);
var expr = AccessComponent(_ca.Expr, evaledIndexes, env);
return expr.Result.IsMatrix(env);
}

public int GetTensorRank(SolusEnvironment env)
{
var evaledIndexes = _ca.GetEvaledIndexes(env);
var expr = AccessComponent(_ca.Expr, evaledIndexes, env);
return expr.Result.GetTensorRank(env);
}

public bool IsString(SolusEnvironment env)
{
var evaledIndexes = _ca.GetEvaledIndexes(env);
var expr = AccessComponent(_ca.Expr, evaledIndexes, env);
return expr.Result.IsString(env);
}

public int GetDimension(SolusEnvironment env, int index)
{
var evaledIndexes = _ca.GetEvaledIndexes(env);
var expr = AccessComponent(_ca.Expr, evaledIndexes, env);
return expr.Result.GetDimension(env, index);
}

public int[] GetDimensions(SolusEnvironment env)
{
var evaledIndexes = _ca.GetEvaledIndexes(env);
var expr = AccessComponent(_ca.Expr, evaledIndexes, env);
return expr.Result.GetDimensions(env);
}

public int GetVectorLength(SolusEnvironment env)
{
var evaledIndexes = _ca.GetEvaledIndexes(env);
var expr = AccessComponent(_ca.Expr, evaledIndexes, env);
return expr.Result.GetVectorLength(env);
}

public bool? IsScalar(SolusEnvironment env) => null;
public bool? IsVector(SolusEnvironment env) => null;
public bool? IsMatrix(SolusEnvironment env) => null;
public int? GetTensorRank(SolusEnvironment env) => null;
public bool? IsString(SolusEnvironment env) => null;
public int? GetDimension(SolusEnvironment env, int index) => null;
public int[] GetDimensions(SolusEnvironment env) => null;
public int? GetVectorLength(SolusEnvironment env) => null;
public bool IsConcrete => false;
}

Expand Down
18 changes: 16 additions & 2 deletions Expressions/FunctionCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public virtual IMathObject Call(SolusEnvironment env)

public virtual List<Expression> Arguments
{
// TODO: make this immutable
get
{
return _arguments;
Expand Down Expand Up @@ -216,7 +217,20 @@ public override string ToString()
}
}

public override IMathObject Result =>
Function.GetResult(Arguments.Select(a => a.Result));
private IMathObject[] _argumentResultCache;

public override IMathObject Result
{
get
{
if (_argumentResultCache == null ||
_argumentResultCache.Length < Arguments.Count)
_argumentResultCache = new IMathObject[Arguments.Count];
int i;
for (i = 0; i < Arguments.Count; i++)
_argumentResultCache[i] = Arguments[i].Result;
return Function.GetResult(_argumentResultCache);
}
}
}
}
21 changes: 8 additions & 13 deletions Expressions/MatrixExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -760,18 +760,17 @@ private class ResultC : IMathObject
{
public ResultC(MatrixExpression me) => _me = me;
private readonly MatrixExpression _me;
public bool IsScalar(SolusEnvironment env) => false;
public bool IsVector(SolusEnvironment env) => false;
public bool IsMatrix(SolusEnvironment env) => true;
public int GetTensorRank(SolusEnvironment env) => _me.TensorRank;
public bool IsString(SolusEnvironment env) => false;
public bool? IsScalar(SolusEnvironment env) => false;
public bool? IsVector(SolusEnvironment env) => false;
public bool? IsMatrix(SolusEnvironment env) => true;
public int? GetTensorRank(SolusEnvironment env) => _me.TensorRank;
public bool? IsString(SolusEnvironment env) => false;

public int GetDimension(SolusEnvironment env, int index)
public int? GetDimension(SolusEnvironment env, int index)
{
if (index == 0) return _me.RowCount;
if (index == 1) return _me.ColumnCount;
throw new IndexOutOfRangeException(
"The index must be zero or one for a matrix");
return null;
}

private int[] __GetDimensions;
Expand All @@ -783,11 +782,7 @@ public int[] GetDimensions(SolusEnvironment env)
return __GetDimensions;
}

public int GetVectorLength(SolusEnvironment env)
{
throw new InvalidOperationException(
"A matrix is not a vector");
}
public int? GetVectorLength(SolusEnvironment env) => null;

public bool IsConcrete => false;
}
Expand Down
32 changes: 8 additions & 24 deletions Expressions/RandomExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,14 @@ public override void AcceptVisitor(IExpressionVisitor visitor)

private class ResultC : IMathObject
{
public bool IsScalar(SolusEnvironment env) => true;
public bool IsVector(SolusEnvironment env) => false;
public bool IsMatrix(SolusEnvironment env) => false;
public int GetTensorRank(SolusEnvironment env) => 0;
public bool IsString(SolusEnvironment env) => false;

public int GetDimension(SolusEnvironment env, int index)
{
throw new IndexOutOfRangeException(
"Random expressions do not have dimensions");
}

public int[] GetDimensions(SolusEnvironment env)
{
throw new IndexOutOfRangeException(
"Random expressions do not have dimensions");
}

public int GetVectorLength(SolusEnvironment env)
{
throw new InvalidOperationException(
"Random expressions do not have a length");
}

public bool? IsScalar(SolusEnvironment env) => true;
public bool? IsVector(SolusEnvironment env) => false;
public bool? IsMatrix(SolusEnvironment env) => false;
public int? GetTensorRank(SolusEnvironment env) => 0;
public bool? IsString(SolusEnvironment env) => false;
public int? GetDimension(SolusEnvironment env, int index) => null;
public int[] GetDimensions(SolusEnvironment env) => null;
public int? GetVectorLength(SolusEnvironment env) => null;
public bool IsConcrete => false;
}
}
Expand Down
Loading

0 comments on commit 226e9fa

Please sign in to comment.