зеркало из https://github.com/dotnet/infer.git
Removed msbuild instructions for mono (#120)
RegexpFormattingSettings ctor sets UseLazyQuantifier. ProductExpOp fix. General code cleanup.
This commit is contained in:
Родитель
a00d7fc462
Коммит
7ec8af439b
17
BUILDING.md
17
BUILDING.md
|
@ -50,15 +50,7 @@ dotnet build -c ReleaseCore Infer.sln
|
|||
```
|
||||
to build release assemblies.
|
||||
|
||||
The corresponding commands to build .NET Standard libraries and .NET Framework executables with Mono are
|
||||
```bash
|
||||
msbuild /c:DebugFull /p:MonoSupport=true /restore Infer.sln
|
||||
```
|
||||
and
|
||||
```bash
|
||||
msbuild /c:ReleaseFull /p:MonoSupport=true /restore Infer.sln
|
||||
```
|
||||
Please, expect build failure messages about examples that use WPF GUI. Libraries and executables that don't reference WPF should build, though.
|
||||
When not using Windows, expect build failure messages about examples that use WPF GUI. Libraries and executables that don't reference WPF should build, though.
|
||||
|
||||
### Run unit tests
|
||||
|
||||
|
@ -137,3 +129,10 @@ When using this special build of Infer.NET, you must tell your code where to fin
|
|||
4. Select the libraries listed in the [MKL linkage guide](https://software.intel.com/en-us/articles/intel-math-kernel-library-intel-mkl-linkage-and-distribution-quick-reference-guide): `mkl_rt.dll`, `mkl_intel_thread.dll`, `mkl_core.dll`, `libiomp5md.dll`, etc.
|
||||
5. Click "Add" or "Add as Link"
|
||||
1. For each of the added items, change the "Copy to output directory" property to "Copy if newer". When you build your project, the MKL libraries will be included alongside the Infer.NET libraries.
|
||||
|
||||
## Embedding Infer.NET source code in your own project
|
||||
|
||||
If you plan to reference the Infer.NET source code from your own project, we recommend using a [Git submodule](https://git-scm.com/book/en/v2/Git-Tools-Submodules). A Git submodule refers to a specific commit of Infer.NET and ensures that anyone who clones your project will get the version of Infer.NET that your code works with. To set up this submodule, you only need to type:
|
||||
```bash
|
||||
git submodule add https://github.com/dotnet/infer
|
||||
```
|
|
@ -132,17 +132,17 @@ namespace Microsoft.ML.Probabilistic.Compiler.Graphs
|
|||
|
||||
public void OnAddNode(NodeType node)
|
||||
{
|
||||
if (AddNode != null) AddNode(node);
|
||||
AddNode?.Invoke(node);
|
||||
}
|
||||
|
||||
public void OnBeginCycle()
|
||||
{
|
||||
if (BeginCycle != null) BeginCycle();
|
||||
BeginCycle?.Invoke();
|
||||
}
|
||||
|
||||
public void OnEndCycle()
|
||||
{
|
||||
if (EndCycle != null) EndCycle();
|
||||
EndCycle?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -325,17 +325,17 @@ namespace Microsoft.ML.Probabilistic.Compiler.Graphs
|
|||
|
||||
public void OnAddEdge(EdgeType edge)
|
||||
{
|
||||
if (AddEdge != null) AddEdge(edge);
|
||||
AddEdge?.Invoke(edge);
|
||||
}
|
||||
|
||||
public void OnBeginCycle()
|
||||
{
|
||||
if (BeginCycle != null) BeginCycle();
|
||||
BeginCycle?.Invoke();
|
||||
}
|
||||
|
||||
public void OnEndCycle()
|
||||
{
|
||||
if (EndCycle != null) EndCycle();
|
||||
EndCycle?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -71,7 +71,7 @@ namespace Microsoft.ML.Probabilistic.Compiler.Graphs
|
|||
|
||||
public void OnSetDistance(NodeType node, int distance)
|
||||
{
|
||||
if (SetDistance != null) SetDistance(node, distance);
|
||||
SetDistance?.Invoke(node, distance);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -68,37 +68,37 @@ namespace Microsoft.ML.Probabilistic.Compiler.Graphs
|
|||
|
||||
protected void OnDiscoverNode(NodeType node)
|
||||
{
|
||||
if (DiscoverNode != null) DiscoverNode(node);
|
||||
DiscoverNode?.Invoke(node);
|
||||
}
|
||||
|
||||
protected void OnFinishNode(NodeType node)
|
||||
{
|
||||
if (FinishNode != null) FinishNode(node);
|
||||
FinishNode?.Invoke(node);
|
||||
}
|
||||
|
||||
protected void OnTreeEdge(EdgeType edge)
|
||||
{
|
||||
if (TreeEdge != null) TreeEdge(edge);
|
||||
TreeEdge?.Invoke(edge);
|
||||
}
|
||||
|
||||
protected void OnBackEdge(EdgeType edge)
|
||||
{
|
||||
if (BackEdge != null) BackEdge(edge);
|
||||
BackEdge?.Invoke(edge);
|
||||
}
|
||||
|
||||
protected void OnCrossEdge(EdgeType edge)
|
||||
{
|
||||
if (CrossEdge != null) CrossEdge(edge);
|
||||
CrossEdge?.Invoke(edge);
|
||||
}
|
||||
|
||||
protected void OnDiscoverEdge(EdgeType edge)
|
||||
{
|
||||
if (DiscoverEdge != null) DiscoverEdge(edge);
|
||||
DiscoverEdge?.Invoke(edge);
|
||||
}
|
||||
|
||||
protected void OnFinishTreeEdge(EdgeType edge)
|
||||
{
|
||||
if (FinishTreeEdge != null) FinishTreeEdge(edge);
|
||||
FinishTreeEdge?.Invoke(edge);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -71,17 +71,17 @@ namespace Microsoft.ML.Probabilistic.Compiler.Graphs
|
|||
|
||||
public void OnAddNode(NodeType node)
|
||||
{
|
||||
if (AddNode != null) AddNode(node);
|
||||
AddNode?.Invoke(node);
|
||||
}
|
||||
|
||||
public void OnBeginPath()
|
||||
{
|
||||
if (BeginPath != null) BeginPath();
|
||||
BeginPath?.Invoke();
|
||||
}
|
||||
|
||||
public void OnEndPath()
|
||||
{
|
||||
if (EndPath != null) EndPath();
|
||||
EndPath?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -158,17 +158,17 @@ namespace Microsoft.ML.Probabilistic.Compiler.Graphs
|
|||
|
||||
public void OnAddEdge(EdgeType edge)
|
||||
{
|
||||
if (AddEdge != null) AddEdge(edge);
|
||||
AddEdge?.Invoke(edge);
|
||||
}
|
||||
|
||||
public void OnBeginPath()
|
||||
{
|
||||
if (BeginPath != null) BeginPath();
|
||||
BeginPath?.Invoke();
|
||||
}
|
||||
|
||||
public void OnEndPath()
|
||||
{
|
||||
if (EndPath != null) EndPath();
|
||||
EndPath?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -85,17 +85,17 @@ namespace Microsoft.ML.Probabilistic.Compiler.Graphs
|
|||
|
||||
public void OnAddNode(NodeType node)
|
||||
{
|
||||
if (AddNode != null) AddNode(node);
|
||||
AddNode?.Invoke(node);
|
||||
}
|
||||
|
||||
public void OnBeginComponent()
|
||||
{
|
||||
if (BeginComponent != null) BeginComponent();
|
||||
BeginComponent?.Invoke();
|
||||
}
|
||||
|
||||
public void OnEndComponent()
|
||||
{
|
||||
if (EndComponent != null) EndComponent();
|
||||
EndComponent?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -205,17 +205,17 @@ namespace Microsoft.ML.Probabilistic.Compiler.Graphs
|
|||
|
||||
public void OnAddNode(NodeType node)
|
||||
{
|
||||
if (AddNode != null) AddNode(node);
|
||||
AddNode?.Invoke(node);
|
||||
}
|
||||
|
||||
public void OnBeginComponent()
|
||||
{
|
||||
if (BeginComponent != null) BeginComponent();
|
||||
BeginComponent?.Invoke();
|
||||
}
|
||||
|
||||
public void OnEndComponent()
|
||||
{
|
||||
if (EndComponent != null) EndComponent();
|
||||
EndComponent?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -753,7 +753,7 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
{
|
||||
if (null == dp)
|
||||
throw new InferCompilerException("A type declaration provider has not been set for this instance of the model compiler");
|
||||
return CompileWithoutParams(dp.GetTypeDeclaration(method != null ? method.DeclaringType : null, true),
|
||||
return CompileWithoutParams(dp.GetTypeDeclaration(method?.DeclaringType, true),
|
||||
method, new AttributeRegistry<object, ICompilerAttribute>(true));
|
||||
}
|
||||
|
||||
|
@ -786,9 +786,7 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
{
|
||||
foreach (CodeTransformer ct in tc.transformers)
|
||||
{
|
||||
DeadCodeTransform bst = ct.Transform as DeadCodeTransform;
|
||||
//SchedulingTransform bst = ct.Transform as SchedulingTransform;
|
||||
if (bst != null)
|
||||
if (ct.Transform is DeadCodeTransform bst)
|
||||
{
|
||||
foreach (ITypeDeclaration itd2 in ct.transformMap.Values)
|
||||
{
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using Microsoft.ML.Probabilistic.Collections;
|
||||
|
||||
namespace Microsoft.ML.Probabilistic.Compiler.Transforms
|
||||
{
|
||||
|
@ -101,9 +100,8 @@ namespace Microsoft.ML.Probabilistic.Compiler.Transforms
|
|||
|
||||
public static bool ContainsType(object container, Type type)
|
||||
{
|
||||
if (container is Type)
|
||||
if (container is Type containerType)
|
||||
{
|
||||
Type containerType = (Type)container;
|
||||
if (containerType.IsGenericTypeDefinition)
|
||||
return type.IsGenericType && containerType.IsAssignableFrom(type.GetGenericTypeDefinition());
|
||||
else return containerType.IsAssignableFrom(type);
|
||||
|
@ -375,16 +373,13 @@ namespace Microsoft.ML.Probabilistic.Compiler.Transforms
|
|||
}
|
||||
else
|
||||
{
|
||||
if (resultIndex != null)
|
||||
if (parameter.IsDefined(typeof(SkipIfMatchingIndexIsUniformAttribute), false))
|
||||
{
|
||||
if (parameter.IsDefined(typeof(SkipIfMatchingIndexIsUniformAttribute), false))
|
||||
{
|
||||
if (resultIndex == null)
|
||||
throw new InferCompilerException(parameter.Name + " has SkipIfMatchingIndexIsUniformAttribute but " + StringUtil.MethodNameToString(method) +
|
||||
" has no resultIndex parameter");
|
||||
IExpression requirement = Builder.ArrayIndex(paramRef, resultIndex);
|
||||
info.Add(DependencyType.SkipIfUniform, Builder.ExprStatement(requirement));
|
||||
}
|
||||
if (resultIndex == null)
|
||||
throw new InferCompilerException(parameter.Name + " has SkipIfMatchingIndexIsUniformAttribute but " + StringUtil.MethodNameToString(method) +
|
||||
" has no resultIndex parameter");
|
||||
IExpression requirement = Builder.ArrayIndex(paramRef, resultIndex);
|
||||
info.Add(DependencyType.SkipIfUniform, Builder.ExprStatement(requirement));
|
||||
}
|
||||
if (parameter.IsDefined(typeof(SkipIfAnyExceptIndexIsUniformAttribute), false))
|
||||
{
|
||||
|
|
|
@ -56,7 +56,7 @@ namespace Microsoft.ML.Probabilistic.Compiler.Transforms
|
|||
protected FactorManager factorManager;
|
||||
protected ModelCompiler compiler;
|
||||
internal bool InitializeOnSeparateLine;
|
||||
bool AllowDerivedParents;
|
||||
readonly bool AllowDerivedParents;
|
||||
|
||||
internal const string resultName = "result";
|
||||
internal const string resultIndexName = "resultIndex";
|
||||
|
@ -85,7 +85,7 @@ namespace Microsoft.ML.Probabilistic.Compiler.Transforms
|
|||
// TODO: consider collapsing unit arrays (arrays of length 1) into non-array variables
|
||||
|
||||
// Caches the quality band of the algorithm
|
||||
private IDictionary<IAlgorithm, QualityBand> algQualityBand = new Dictionary<IAlgorithm, QualityBand>();
|
||||
private readonly IDictionary<IAlgorithm, QualityBand> algQualityBand = new Dictionary<IAlgorithm, QualityBand>();
|
||||
|
||||
public MessageTransform(ModelCompiler compiler, IAlgorithm algorithm, FactorManager factorManager, bool allowDerivedParents)
|
||||
{
|
||||
|
@ -553,7 +553,7 @@ namespace Microsoft.ML.Probabilistic.Compiler.Transforms
|
|||
if (argIsConstant[i] && !isChild) continue;
|
||||
if (UseMessageAnalysis)
|
||||
{
|
||||
AddInitialiserStatement(mi.messageFromFactor, (iae == null) ? (IExpression)imie : iae);
|
||||
AddInitialiserStatement(mi.messageFromFactor, iae ?? (IExpression)imie);
|
||||
if (isChild) AddInitialiserStatement(mi.messageToFactor, null);
|
||||
}
|
||||
|
||||
|
@ -603,11 +603,11 @@ namespace Microsoft.ML.Probabilistic.Compiler.Transforms
|
|||
this.factorManager.GivePriorityTo(op.Container);
|
||||
}
|
||||
|
||||
Action<IStatement> action = delegate (IStatement st)
|
||||
void action(IStatement st)
|
||||
{
|
||||
if (argIsConstant[i]) context.OutputAttributes.Remove<OperatorStatement>(st);
|
||||
context.AddStatementBeforeCurrent(st);
|
||||
};
|
||||
}
|
||||
ForEachOperatorStatement(action, alg, info, msgInfo, operatorSuffix, targetParameter, argumentTypes, isStochastic, isVariableFactor);
|
||||
|
||||
this.factorManager.PriorityList.RemoveRange(0, this.factorManager.PriorityList.Count - currentPriorityListSize);
|
||||
|
@ -806,12 +806,12 @@ namespace Microsoft.ML.Probabilistic.Compiler.Transforms
|
|||
});
|
||||
if (!hasReturnParameter) throw new NotSupportedException("'" + returnValue + "' is not an argument of " + StringUtil.MethodSignatureToString(fninfo.Method));
|
||||
}
|
||||
Predicate<string> conversionAllowed = parameterName =>
|
||||
bool conversionAllowed(string parameterName)
|
||||
{
|
||||
// Do not allow point mass conversion of the return parameter in a LogEvidenceRatio method.
|
||||
return targetParameter.Length > 0 || info.IsVoid || fninfo.Method.Name != epEvidenceMethodName ||
|
||||
(fninfo.factorEdgeOfParameter[parameterName].ParameterName != info.ParameterNames[0]);
|
||||
};
|
||||
}
|
||||
fullArgs = ConvertArguments(fninfo.Method, fullArgs, conversionAllowed);
|
||||
IExpression operatorMethod = Builder.StaticMethod(fninfo.Method, fullArgs.ToArray());
|
||||
if (!context.OutputAttributes.Has<MessageFcnInfo>(fninfo.Method))
|
||||
|
@ -1243,9 +1243,8 @@ namespace Microsoft.ML.Probabilistic.Compiler.Transforms
|
|||
|
||||
protected override IExpression ConvertAssign(IAssignExpression iae)
|
||||
{
|
||||
if (iae.Target is IVariableDeclarationExpression)
|
||||
if (iae.Target is IVariableDeclarationExpression ivde)
|
||||
{
|
||||
IVariableDeclarationExpression ivde = (IVariableDeclarationExpression)iae.Target;
|
||||
if (IsStochasticVariableReference(ivde))
|
||||
{
|
||||
if (false)
|
||||
|
@ -1982,9 +1981,8 @@ namespace Microsoft.ML.Probabilistic.Compiler.Transforms
|
|||
|
||||
private Type GetInitializerDomainType(IExpression initialiser, out int minusDepth)
|
||||
{
|
||||
if (initialiser is IArrayIndexerExpression)
|
||||
if (initialiser is IArrayIndexerExpression iaie)
|
||||
{
|
||||
IArrayIndexerExpression iaie = (IArrayIndexerExpression)initialiser;
|
||||
Type type = GetInitializerDomainType(iaie.Target, out minusDepth);
|
||||
if (minusDepth > 0)
|
||||
{
|
||||
|
@ -1995,9 +1993,8 @@ namespace Microsoft.ML.Probabilistic.Compiler.Transforms
|
|||
}
|
||||
else
|
||||
{
|
||||
if (initialiser is IArrayCreateExpression)
|
||||
if (initialiser is IArrayCreateExpression iace)
|
||||
{
|
||||
IArrayCreateExpression iace = (IArrayCreateExpression)initialiser;
|
||||
if (iace.Type.DotNetType.Equals(typeof(PlaceHolder)))
|
||||
{
|
||||
Type type = GetInitializerDomainType(iace.Initializer.Expressions[0], out minusDepth);
|
||||
|
@ -2025,13 +2022,11 @@ namespace Microsoft.ML.Probabilistic.Compiler.Transforms
|
|||
{
|
||||
Type type;
|
||||
if (initialiserType.TryGetValue(initialiser, out type)) return Builder.CastExpr(initialiser, type);
|
||||
if (initialiser is IArrayIndexerExpression)
|
||||
if (initialiser is IArrayIndexerExpression iaie)
|
||||
{
|
||||
IArrayIndexerExpression iaie = (IArrayIndexerExpression)initialiser;
|
||||
IExpression target = ConvertInitialiser(iaie.Target);
|
||||
if (target is IArrayCreateExpression)
|
||||
if (target is IArrayCreateExpression iace)
|
||||
{
|
||||
IArrayCreateExpression iace = (IArrayCreateExpression)target;
|
||||
if (iace.Type.DotNetType.Equals(typeof(PlaceHolder)) && iace.Initializer != null && iace.Initializer.Expressions.Count == 1)
|
||||
{
|
||||
IExpression initExpr = iace.Initializer.Expressions[0];
|
||||
|
@ -2045,9 +2040,8 @@ namespace Microsoft.ML.Probabilistic.Compiler.Transforms
|
|||
}
|
||||
return Builder.ArrayIndex(target, iaie.Indices);
|
||||
}
|
||||
else if (initialiser is IArrayCreateExpression)
|
||||
else if (initialiser is IArrayCreateExpression iace)
|
||||
{
|
||||
IArrayCreateExpression iace = (IArrayCreateExpression)initialiser;
|
||||
if (iace.Initializer == null) return initialiser;
|
||||
IArrayCreateExpression ace = Builder.ArrayCreateExpr(iace.Type, iace.Dimensions);
|
||||
ace.Initializer = Builder.BlockExpr();
|
||||
|
@ -2095,9 +2089,8 @@ namespace Microsoft.ML.Probabilistic.Compiler.Transforms
|
|||
/// <returns></returns>
|
||||
private IExpression ConvertInitialiser(IExpression initialiser, Type desiredType, VariableInformation varInfo, int depth = 0, bool makeCopy = true)
|
||||
{
|
||||
if (initialiser is IArrayCreateExpression)
|
||||
if (initialiser is IArrayCreateExpression iace)
|
||||
{
|
||||
IArrayCreateExpression iace = (IArrayCreateExpression)initialiser;
|
||||
if (iace.Initializer != null && iace.Initializer.Expressions.Count == 1)
|
||||
{
|
||||
Type desiredElementType = Util.GetElementType(desiredType);
|
||||
|
@ -2119,9 +2112,8 @@ namespace Microsoft.ML.Probabilistic.Compiler.Transforms
|
|||
{
|
||||
return GetSamplingExpression(initialiser);
|
||||
}
|
||||
if (initialiser is IArrayIndexerExpression)
|
||||
if (initialiser is IArrayIndexerExpression iaie)
|
||||
{
|
||||
IArrayIndexerExpression iaie = (IArrayIndexerExpression)initialiser;
|
||||
Type listType = typeof(IList<>).MakeGenericType(desiredType);
|
||||
IExpression target = ConvertInitialiser(iaie.Target, listType, varInfo, depth - 1, false);
|
||||
initialiser = Builder.ArrayIndex(target, iaie.Indices);
|
||||
|
@ -2504,7 +2496,7 @@ namespace Microsoft.ML.Probabilistic.Compiler.Transforms
|
|||
VariableInformation vi = VariableInformation.GetVariableInformation(context, ci.decl);
|
||||
// leading array is [] up to distArraysDepth, then distribution arrays
|
||||
int distArraysDepth = vi.LiteralIndexingDepth;
|
||||
Predicate<int> useFileArrayAtDepth = depth => vi.IsPartitionedAtDepth(context, depth);
|
||||
bool useFileArrayAtDepth(int depth) => vi.IsPartitionedAtDepth(context, depth);
|
||||
for (int depth = 0; depth < distArraysDepth; depth++)
|
||||
{
|
||||
if (useFileArrayAtDepth(depth))
|
||||
|
|
|
@ -5470,20 +5470,17 @@ namespace Microsoft.ML.Probabilistic.Compiler.Transforms
|
|||
public void OnAddEdge(EdgeType edge)
|
||||
{
|
||||
excluded[edge] = true;
|
||||
if (AddEdge != null)
|
||||
AddEdge(edge);
|
||||
AddEdge?.Invoke(edge);
|
||||
}
|
||||
|
||||
public void OnBeginCycle()
|
||||
{
|
||||
if (BeginCycle != null)
|
||||
BeginCycle();
|
||||
BeginCycle?.Invoke();
|
||||
}
|
||||
|
||||
public void OnEndCycle()
|
||||
{
|
||||
if (EndCycle != null)
|
||||
EndCycle();
|
||||
EndCycle?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -66,16 +66,16 @@ namespace Microsoft.ML.Probabilistic.Compiler.Visualizers
|
|||
class DotWriteHelper : WriteHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Write atribute of DOT object.
|
||||
/// Write attribute of DOT object.
|
||||
/// </summary>
|
||||
/// <param name="writer">Text writer assigned to a DOT file.</param>
|
||||
/// <param name="name">Name of the atribute.</param>
|
||||
/// <param name="defaultValue">Default value of the atribute.</param>
|
||||
/// <param name="value">Value of the atribute. If it equals default value, attribute isn't written.</param>
|
||||
/// <param name="converter">Converter that represent value of atribute as a string. If null, ToString() method of value is used.</param>
|
||||
/// <param name="name">Name of the attribute.</param>
|
||||
/// <param name="defaultValue">Default value of the attribute.</param>
|
||||
/// <param name="value">Value of the attribute. If it equals default value or null, the attribute isn't written.</param>
|
||||
/// <param name="converter">Converter that represent value of attribute as a string. If null, ToString() method of value is used.</param>
|
||||
public static void WriteAttribute(TextWriter writer, string name, object defaultValue, object value, Func<object, string> converter = null)
|
||||
{
|
||||
if (defaultValue == null && value == null || value.Equals(defaultValue))
|
||||
if (value == null || value.Equals(defaultValue))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -466,8 +466,7 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
/// <returns>Expression statement</returns>
|
||||
public IExpressionStatement ExprStatement(IExpression expr)
|
||||
{
|
||||
if (expr == null)
|
||||
throw new ArgumentException("Supplied expression was null.");
|
||||
if (expr == null) throw new ArgumentNullException(nameof(expr));
|
||||
IExpressionStatement es = ExprStatement();
|
||||
es.Expression = expr;
|
||||
return es;
|
||||
|
@ -514,12 +513,11 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
public ITypeReference TypeRef(string name, Type t, IType outerClass, params IType[] typeArguments)
|
||||
{
|
||||
ITypeReference tir = TypeInstRef();
|
||||
IDotNetType tir_dn = tir as IDotNetType;
|
||||
foreach (IType tp in typeArguments) tir.GenericArguments.Add(tp);
|
||||
string s = "`" + typeArguments.Length;
|
||||
if (name.EndsWith(s)) name = name.Substring(0, name.Length - s.Length);
|
||||
tir.GenericType = (ITypeReference)TypeRef(name, t, outerClass);
|
||||
if (tir_dn != null)
|
||||
if (tir is IDotNetType tir_dn)
|
||||
tir_dn.DotNetType = t;
|
||||
return tir;
|
||||
}
|
||||
|
@ -534,7 +532,6 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
public IType TypeRef(string name, Type t, IType outerClass)
|
||||
{
|
||||
ITypeReference tr = TypeRef();
|
||||
IDotNetType tr_dn = tr as IDotNetType;
|
||||
name = name.Replace('+', '.');
|
||||
bool isByRef = (name.EndsWith("&"));
|
||||
if (isByRef) name = name.Substring(0, name.Length - 1);
|
||||
|
@ -549,15 +546,14 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
if (outerClass != null)
|
||||
tr.Owner = outerClass;
|
||||
|
||||
if (tr_dn != null)
|
||||
if (tr is IDotNetType tr_dn)
|
||||
tr_dn.DotNetType = t;
|
||||
|
||||
if (isByRef)
|
||||
{
|
||||
IReferenceType rt = RefType();
|
||||
rt.ElementType = tr;
|
||||
IDotNetType rt_dn = rt as IDotNetType;
|
||||
if (rt_dn != null)
|
||||
if (rt is IDotNetType rt_dn)
|
||||
rt_dn.DotNetType = t;
|
||||
return rt;
|
||||
}
|
||||
|
@ -1081,8 +1077,7 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
// at.Dimensions.Add(ArrayDim());
|
||||
//}
|
||||
// Set up the dotNet type for the array
|
||||
IDotNetType idn = at as IDotNetType;
|
||||
if (idn != null)
|
||||
if (at is IDotNetType idn)
|
||||
{
|
||||
Type elementType = ToType(type);
|
||||
idn.DotNetType = MakeArrayType(elementType, rank);
|
||||
|
@ -1244,7 +1239,7 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
/// <returns>Variable reference expression</returns>
|
||||
public IVariableReferenceExpression VarRefExpr(IVariableReference ivr)
|
||||
{
|
||||
if (ivr == null) throw new ArgumentException("Null variable reference supplied.");
|
||||
if (ivr == null) throw new ArgumentNullException(nameof(ivr));
|
||||
IVariableReferenceExpression vre = VarRefExpr();
|
||||
vre.Variable = ivr;
|
||||
return vre;
|
||||
|
@ -1270,8 +1265,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
/// <returns>Assignment expression</returns>
|
||||
public IAssignExpression AssignExpr(IExpression target, IExpression expr)
|
||||
{
|
||||
if (target == null) throw new NullReferenceException("Assignment target was null.");
|
||||
if (expr == null) throw new NullReferenceException("Assignment expression was null.");
|
||||
if (target == null) throw new ArgumentNullException(nameof(target));
|
||||
if (expr == null) throw new ArgumentNullException(nameof(expr));
|
||||
IAssignExpression ae = AssignExpr();
|
||||
ae.Target = target;
|
||||
ae.Expression = expr;
|
||||
|
@ -1480,31 +1475,27 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
/// <returns>The resulting expression</returns>
|
||||
public IExpression ReplaceVariable(IExpression expr, IVariableDeclaration ivdFind, IVariableDeclaration ivdReplace)
|
||||
{
|
||||
if (expr is IVariableReferenceExpression)
|
||||
if (expr is IVariableReferenceExpression ivre)
|
||||
{
|
||||
IVariableReferenceExpression ivre = (IVariableReferenceExpression)expr;
|
||||
if (ivre.Variable.Resolve() == ivdFind) return VarRefExpr(ivdReplace);
|
||||
return ivre;
|
||||
}
|
||||
else if (expr is IArrayIndexerExpression)
|
||||
else if (expr is IArrayIndexerExpression iaie)
|
||||
{
|
||||
IArrayIndexerExpression iaie = (IArrayIndexerExpression)expr;
|
||||
IArrayIndexerExpression aie = ArrayIndxrExpr();
|
||||
foreach (IExpression ind in iaie.Indices) aie.Indices.Add(ReplaceVariable(ind, ivdFind, ivdReplace));
|
||||
aie.Target = ReplaceVariable(iaie.Target, ivdFind, ivdReplace);
|
||||
return aie;
|
||||
}
|
||||
else if (expr is IVariableDeclarationExpression)
|
||||
else if (expr is IVariableDeclarationExpression ivde)
|
||||
{
|
||||
IVariableDeclarationExpression ivde = (IVariableDeclarationExpression)expr;
|
||||
if (ivde.Variable == ivdFind) return VarDeclExpr(ivdReplace);
|
||||
return ivde;
|
||||
}
|
||||
else if (expr is ILiteralExpression) return expr;
|
||||
else if (expr is IArgumentReferenceExpression) return expr;
|
||||
else if (expr is IPropertyReferenceExpression)
|
||||
else if (expr is IPropertyReferenceExpression ipre)
|
||||
{
|
||||
IPropertyReferenceExpression ipre = (IPropertyReferenceExpression)expr;
|
||||
IExpression target = ReplaceVariable(ipre.Target, ivdFind, ivdReplace);
|
||||
if (target == ipre.Target) return ipre;
|
||||
IPropertyReferenceExpression pre = PropRefExpr();
|
||||
|
@ -1525,9 +1516,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
/// <returns>The resulting expression</returns>
|
||||
public IExpression ReplaceVariable(IExpression expr, IVariableDeclaration ivdFind, IExpression exprReplace, ref int replaceCount)
|
||||
{
|
||||
if (expr is IVariableReferenceExpression)
|
||||
if (expr is IVariableReferenceExpression ivre)
|
||||
{
|
||||
IVariableReferenceExpression ivre = (IVariableReferenceExpression)expr;
|
||||
if (ivre.Variable.Resolve() == ivdFind)
|
||||
{
|
||||
replaceCount++;
|
||||
|
@ -1535,17 +1525,15 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
}
|
||||
return ivre;
|
||||
}
|
||||
else if (expr is IArrayIndexerExpression)
|
||||
else if (expr is IArrayIndexerExpression iaie)
|
||||
{
|
||||
IArrayIndexerExpression iaie = (IArrayIndexerExpression)expr;
|
||||
IArrayIndexerExpression aie = ArrayIndxrExpr();
|
||||
foreach (IExpression ind in iaie.Indices) aie.Indices.Add(ReplaceVariable(ind, ivdFind, exprReplace, ref replaceCount));
|
||||
aie.Target = ReplaceVariable(iaie.Target, ivdFind, exprReplace, ref replaceCount);
|
||||
return aie;
|
||||
}
|
||||
else if (expr is IVariableDeclarationExpression)
|
||||
else if (expr is IVariableDeclarationExpression ivde)
|
||||
{
|
||||
IVariableDeclarationExpression ivde = (IVariableDeclarationExpression)expr;
|
||||
if (ivde.Variable == ivdFind)
|
||||
{
|
||||
replaceCount++;
|
||||
|
@ -1555,9 +1543,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
}
|
||||
else if (expr is ILiteralExpression) return expr;
|
||||
else if (expr is IArgumentReferenceExpression) return expr;
|
||||
else if (expr is IPropertyReferenceExpression)
|
||||
else if (expr is IPropertyReferenceExpression ipre)
|
||||
{
|
||||
IPropertyReferenceExpression ipre = (IPropertyReferenceExpression)expr;
|
||||
IExpression target = ReplaceVariable(ipre.Target, ivdFind, exprReplace, ref replaceCount);
|
||||
if (target == ipre.Target) return ipre;
|
||||
IPropertyReferenceExpression pre = PropRefExpr();
|
||||
|
@ -1610,31 +1597,27 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
replaceCount++;
|
||||
return exprReplace;
|
||||
}
|
||||
else if (expr is IArrayIndexerExpression)
|
||||
else if (expr is IArrayIndexerExpression iaie)
|
||||
{
|
||||
IArrayIndexerExpression iaie = (IArrayIndexerExpression)expr;
|
||||
IArrayIndexerExpression aie = ArrayIndxrExpr();
|
||||
foreach (IExpression ind in iaie.Indices) aie.Indices.Add(ReplaceExpression(ind, exprFind, exprReplace, ref replaceCount));
|
||||
aie.Target = ReplaceExpression(iaie.Target, exprFind, exprReplace, ref replaceCount);
|
||||
return aie;
|
||||
}
|
||||
else if (expr is IPropertyIndexerExpression)
|
||||
else if (expr is IPropertyIndexerExpression ipie)
|
||||
{
|
||||
IPropertyIndexerExpression ipie = (IPropertyIndexerExpression)expr;
|
||||
IPropertyIndexerExpression pie = PropIndxrExpr();
|
||||
foreach (IExpression ind in ipie.Indices) pie.Indices.Add(ReplaceExpression(ind, exprFind, exprReplace, ref replaceCount));
|
||||
pie.Target = (IPropertyReferenceExpression)ReplaceExpression(ipie.Target, exprFind, exprReplace, ref replaceCount);
|
||||
return pie;
|
||||
}
|
||||
else if (expr is ICastExpression)
|
||||
else if (expr is ICastExpression ice)
|
||||
{
|
||||
ICastExpression ice = (ICastExpression)expr;
|
||||
return CastExpr(ReplaceExpression(ice.Expression, exprFind, exprReplace, ref replaceCount), ice.TargetType);
|
||||
}
|
||||
else if (expr is ICheckedExpression)
|
||||
else if (expr is ICheckedExpression iche)
|
||||
{
|
||||
ICheckedExpression ice = (ICheckedExpression)expr;
|
||||
return CheckedExpr(ReplaceExpression(ice.Expression, exprFind, exprReplace, ref replaceCount));
|
||||
return CheckedExpr(ReplaceExpression(iche.Expression, exprFind, exprReplace, ref replaceCount));
|
||||
}
|
||||
else if (
|
||||
(expr is IVariableDeclarationExpression) ||
|
||||
|
@ -1642,9 +1625,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
(expr is ILiteralExpression) ||
|
||||
(expr is IDefaultExpression) ||
|
||||
(expr is IArgumentReferenceExpression)) return expr;
|
||||
else if (expr is IPropertyReferenceExpression)
|
||||
else if (expr is IPropertyReferenceExpression ipre)
|
||||
{
|
||||
IPropertyReferenceExpression ipre = (IPropertyReferenceExpression)expr;
|
||||
IExpression target = ReplaceExpression(ipre.Target, exprFind, exprReplace, ref replaceCount);
|
||||
if (target == ipre.Target) return ipre;
|
||||
IPropertyReferenceExpression pre = PropRefExpr();
|
||||
|
@ -1664,19 +1646,17 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
}
|
||||
return ace;
|
||||
}
|
||||
else if (expr is IBlockExpression)
|
||||
else if (expr is IBlockExpression ible)
|
||||
{
|
||||
IBlockExpression ibe = (IBlockExpression)expr;
|
||||
IBlockExpression be = BlockExpr();
|
||||
foreach (IExpression e in ibe.Expressions)
|
||||
foreach (IExpression e in ible.Expressions)
|
||||
{
|
||||
be.Expressions.Add(ReplaceExpression(e, exprFind, exprReplace, ref replaceCount));
|
||||
}
|
||||
return be;
|
||||
}
|
||||
else if (expr is IMethodInvokeExpression)
|
||||
else if (expr is IMethodInvokeExpression imie)
|
||||
{
|
||||
IMethodInvokeExpression imie = (IMethodInvokeExpression)expr;
|
||||
IMethodInvokeExpression mie = MethodInvkExpr();
|
||||
mie.Method = imie.Method;
|
||||
foreach (IExpression arg in imie.Arguments)
|
||||
|
@ -1685,9 +1665,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
}
|
||||
return mie;
|
||||
}
|
||||
else if (expr is IObjectCreateExpression)
|
||||
else if (expr is IObjectCreateExpression ioce)
|
||||
{
|
||||
IObjectCreateExpression ioce = (IObjectCreateExpression)expr;
|
||||
IObjectCreateExpression oce = ObjCreateExpr();
|
||||
oce.Constructor = ioce.Constructor;
|
||||
oce.Type = ioce.Type;
|
||||
|
@ -1698,9 +1677,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
oce.Initializer = (IBlockExpression)ReplaceExpression(ioce.Initializer, exprFind, exprReplace, ref replaceCount);
|
||||
return oce;
|
||||
}
|
||||
else if (expr is IAnonymousMethodExpression)
|
||||
else if (expr is IAnonymousMethodExpression iame)
|
||||
{
|
||||
IAnonymousMethodExpression iame = (IAnonymousMethodExpression)expr;
|
||||
IAnonymousMethodExpression ame = AnonMethodExpr();
|
||||
ame.DelegateType = iame.DelegateType;
|
||||
foreach (IParameterDeclaration ipd in iame.Parameters) ame.Parameters.Add(ipd);
|
||||
|
@ -1708,40 +1686,35 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
foreach (IStatement ist in iame.Body.Statements)
|
||||
{
|
||||
IStatement st = ist;
|
||||
if (ist is IExpressionStatement)
|
||||
if (ist is IExpressionStatement ies)
|
||||
{
|
||||
IExpressionStatement ies = (IExpressionStatement)ist;
|
||||
st = ExprStatement(ReplaceExpression(ies.Expression, exprFind, exprReplace, ref replaceCount));
|
||||
}
|
||||
else if (ist is IMethodReturnStatement)
|
||||
else if (ist is IMethodReturnStatement imrs)
|
||||
{
|
||||
IMethodReturnStatement imrs = (IMethodReturnStatement)ist;
|
||||
st = Return(ReplaceExpression(imrs.Expression, exprFind, exprReplace, ref replaceCount));
|
||||
}
|
||||
ame.Body.Statements.Add(st);
|
||||
}
|
||||
return ame;
|
||||
}
|
||||
else if (expr is IUnaryExpression)
|
||||
else if (expr is IUnaryExpression iue)
|
||||
{
|
||||
IUnaryExpression iue = (IUnaryExpression)expr;
|
||||
IUnaryExpression ue = UnaryExpr();
|
||||
ue.Operator = iue.Operator;
|
||||
ue.Expression = ReplaceExpression(iue.Expression, exprFind, exprReplace, ref replaceCount);
|
||||
return ue;
|
||||
}
|
||||
else if (expr is IBinaryExpression)
|
||||
else if (expr is IBinaryExpression ibe)
|
||||
{
|
||||
IBinaryExpression ibe = (IBinaryExpression)expr;
|
||||
IBinaryExpression be = BinaryExpr();
|
||||
be.Operator = ibe.Operator;
|
||||
be.Left = ReplaceExpression(ibe.Left, exprFind, exprReplace, ref replaceCount);
|
||||
be.Right = ReplaceExpression(ibe.Right, exprFind, exprReplace, ref replaceCount);
|
||||
return be;
|
||||
}
|
||||
else if (expr is IMethodReferenceExpression)
|
||||
else if (expr is IMethodReferenceExpression imre)
|
||||
{
|
||||
var imre = (IMethodReferenceExpression)expr;
|
||||
var mre = MethodRefExpr();
|
||||
mre.Method = imre.Method;
|
||||
mre.Target = ReplaceExpression(imre.Target, exprFind, exprReplace, ref replaceCount);
|
||||
|
|
|
@ -170,10 +170,10 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
int methodIndex = context.FindAncestorIndex<IMethodInvokeExpression>();
|
||||
if (methodIndex != -1)
|
||||
{
|
||||
IAddressOutExpression iaoe = context.GetAncestor(methodIndex + 1) as IAddressOutExpression;
|
||||
if (iaoe == null)
|
||||
if (context.GetAncestor(methodIndex + 1) is IAddressOutExpression iaoe)
|
||||
return IsPartOf(iaoe.Expression, expr);
|
||||
else
|
||||
return false;
|
||||
return IsPartOf(iaoe.Expression, expr);
|
||||
}
|
||||
return IsOnLHSOfAssignment(context, expr);
|
||||
}
|
||||
|
@ -187,9 +187,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
if (bounds.TryGetValue(ivd, out b))
|
||||
return b;
|
||||
}
|
||||
else if (expr is IBinaryExpression)
|
||||
else if (expr is IBinaryExpression ibe)
|
||||
{
|
||||
IBinaryExpression ibe = (IBinaryExpression)expr;
|
||||
Bounds left = GetBounds(ibe.Left, bounds);
|
||||
Bounds right = GetBounds(ibe.Right, bounds);
|
||||
if (left != null && right != null)
|
||||
|
@ -212,12 +211,10 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
}
|
||||
}
|
||||
}
|
||||
else if (expr is ILiteralExpression)
|
||||
else if (expr is ILiteralExpression ile)
|
||||
{
|
||||
ILiteralExpression ile = (ILiteralExpression)expr;
|
||||
if (ile.Value is int)
|
||||
if (ile.Value is int value)
|
||||
{
|
||||
int value = (int)ile.Value;
|
||||
return new Bounds()
|
||||
{
|
||||
lowerBound = value,
|
||||
|
@ -241,12 +238,10 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
{
|
||||
return GetVariableDeclaration(expr);
|
||||
}
|
||||
else if (expr is IBinaryExpression)
|
||||
else if (expr is IBinaryExpression indexBinaryExpr)
|
||||
{
|
||||
IBinaryExpression indexBinaryExpr = (IBinaryExpression)expr;
|
||||
IVariableReferenceExpression ivre = indexBinaryExpr.Left as IVariableReferenceExpression;
|
||||
ILiteralExpression offsetExpr = indexBinaryExpr.Right as ILiteralExpression;
|
||||
if (ivre != null && offsetExpr != null && offsetExpr.Value is int)
|
||||
if (indexBinaryExpr.Left is IVariableReferenceExpression ivre && offsetExpr != null && offsetExpr.Value is int)
|
||||
{
|
||||
offset = (int)offsetExpr.Value;
|
||||
if (indexBinaryExpr.Operator == BinaryOperator.Subtract)
|
||||
|
@ -270,7 +265,7 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
/// <summary>
|
||||
/// Reflection cache
|
||||
/// </summary>
|
||||
MethodInfo AnyIndexMethod = new Func<int>(GateAnalysisTransform.AnyIndex).Method;
|
||||
readonly MethodInfo AnyIndexMethod = new Func<int>(GateAnalysisTransform.AnyIndex).Method;
|
||||
|
||||
// helper for MutatingFirstAffectsSecond
|
||||
// offsets and extraIndices only need to be modified on a match (though they can be modified in any case)
|
||||
|
@ -451,7 +446,7 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
/// <summary>
|
||||
/// Reflection cache
|
||||
/// </summary>
|
||||
MethodInfo AnyMethod = new Func<object[], object>(FactorManager.Any).Method;
|
||||
readonly MethodInfo AnyMethod = new Func<object[], object>(FactorManager.Any).Method;
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if mutating the first expression would affect the value of the second.
|
||||
|
@ -537,12 +532,10 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
bool lastBracket = (i == affectedPrefixes.Count - 1);
|
||||
IExpression mutatedPrefix = mutatedPrefixes[i];
|
||||
IExpression affectedPrefix = affectedPrefixes[i];
|
||||
if (mutatedPrefix is IArrayIndexerExpression)
|
||||
if (mutatedPrefix is IArrayIndexerExpression mutated_iaie)
|
||||
{
|
||||
IArrayIndexerExpression mutated_iaie = (IArrayIndexerExpression)mutatedPrefix;
|
||||
if (affectedPrefix is IArrayIndexerExpression)
|
||||
if (affectedPrefix is IArrayIndexerExpression affected_iaie)
|
||||
{
|
||||
IArrayIndexerExpression affected_iaie = (IArrayIndexerExpression)affectedPrefix;
|
||||
try
|
||||
{
|
||||
if (!IndicesOverlap(mutated_iaie.Indices, affected_iaie.Indices, mutatesWithinOnly && lastBracket, boundsInMutated, boundsInAffected, offsets, extraIndices, matchedIndices))
|
||||
|
@ -556,36 +549,30 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
else
|
||||
return !mutatesWithinOnly;
|
||||
}
|
||||
else if (mutatedPrefix is IPropertyIndexerExpression)
|
||||
else if (mutatedPrefix is IPropertyIndexerExpression mutated_ipie)
|
||||
{
|
||||
IPropertyIndexerExpression mutated_ipie = (IPropertyIndexerExpression)mutatedPrefix;
|
||||
if (affectedPrefix is IPropertyIndexerExpression)
|
||||
if (affectedPrefix is IPropertyIndexerExpression affected_ipie)
|
||||
{
|
||||
IPropertyIndexerExpression affected_ipie = (IPropertyIndexerExpression)affectedPrefix;
|
||||
if (!IndicesOverlap(mutated_ipie.Indices, affected_ipie.Indices, mutatesWithinOnly && lastBracket, boundsInMutated, boundsInAffected, offsets, extraIndices, matchedIndices))
|
||||
return false;
|
||||
}
|
||||
else
|
||||
return !mutatesWithinOnly;
|
||||
}
|
||||
else if (mutatedPrefix is IPropertyReferenceExpression)
|
||||
else if (mutatedPrefix is IPropertyReferenceExpression mutated_ipre)
|
||||
{
|
||||
IPropertyReferenceExpression mutated_ipre = (IPropertyReferenceExpression)mutatedPrefix;
|
||||
if (affectedPrefix is IPropertyReferenceExpression)
|
||||
if (affectedPrefix is IPropertyReferenceExpression affected_ipre)
|
||||
{
|
||||
IPropertyReferenceExpression affected_ipre = (IPropertyReferenceExpression)affectedPrefix;
|
||||
if (!mutated_ipre.Property.Equals(affected_ipre.Property))
|
||||
return !mutatesWithinOnly;
|
||||
}
|
||||
else
|
||||
return !mutatesWithinOnly;
|
||||
}
|
||||
else if (mutatedPrefix is IFieldReferenceExpression)
|
||||
else if (mutatedPrefix is IFieldReferenceExpression mutated_ifre)
|
||||
{
|
||||
IFieldReferenceExpression mutated_ifre = (IFieldReferenceExpression)mutatedPrefix;
|
||||
if (affectedPrefix is IFieldReferenceExpression)
|
||||
if (affectedPrefix is IFieldReferenceExpression affected_ifre)
|
||||
{
|
||||
IFieldReferenceExpression affected_ifre = (IFieldReferenceExpression)affectedPrefix;
|
||||
if (!mutated_ifre.Field.Equals(affected_ifre.Field))
|
||||
return !mutatesWithinOnly;
|
||||
}
|
||||
|
@ -604,18 +591,16 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
Set<IVariableDeclaration> loopVars = new Set<IVariableDeclaration>(new IdentityComparer<IVariableDeclaration>());
|
||||
foreach (IStatement container in containers.inputs)
|
||||
{
|
||||
if (container is IForStatement)
|
||||
if (container is IForStatement ifs)
|
||||
{
|
||||
IForStatement ifs = (IForStatement)container;
|
||||
loopVars.Add(LoopVariable(ifs));
|
||||
}
|
||||
}
|
||||
for (int i = count; i < mutatedPrefixes.Count; i++)
|
||||
{
|
||||
IExpression mutatedPrefix = mutatedPrefixes[i];
|
||||
if (mutatedPrefix is IArrayIndexerExpression)
|
||||
if (mutatedPrefix is IArrayIndexerExpression mutated_iaie)
|
||||
{
|
||||
IArrayIndexerExpression mutated_iaie = (IArrayIndexerExpression)mutatedPrefix;
|
||||
foreach (IExpression index in mutated_iaie.Indices)
|
||||
{
|
||||
foreach (var v in GetVariables(index))
|
||||
|
@ -685,12 +670,10 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
{
|
||||
IExpression prefix1 = prefixes1[i];
|
||||
IExpression prefix2 = prefixes2[i];
|
||||
if (prefix1 is IArrayIndexerExpression)
|
||||
if (prefix1 is IArrayIndexerExpression iaie1)
|
||||
{
|
||||
IArrayIndexerExpression iaie1 = (IArrayIndexerExpression)prefix1;
|
||||
if (prefix2 is IArrayIndexerExpression)
|
||||
if (prefix2 is IArrayIndexerExpression iaie2)
|
||||
{
|
||||
IArrayIndexerExpression iaie2 = (IArrayIndexerExpression)prefix2;
|
||||
if (iaie1.Indices.Count != iaie2.Indices.Count)
|
||||
break;
|
||||
for (int j = 0; j < iaie1.Indices.Count; j++)
|
||||
|
@ -712,12 +695,10 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
else
|
||||
break;
|
||||
}
|
||||
else if (prefix1 is IPropertyIndexerExpression)
|
||||
else if (prefix1 is IPropertyIndexerExpression ipie1)
|
||||
{
|
||||
IPropertyIndexerExpression ipie1 = (IPropertyIndexerExpression)prefix1;
|
||||
if (prefix2 is IPropertyIndexerExpression)
|
||||
if (prefix2 is IPropertyIndexerExpression ipie2)
|
||||
{
|
||||
IPropertyIndexerExpression ipie2 = (IPropertyIndexerExpression)prefix2;
|
||||
if (ipie1.Indices.Count != ipie2.Indices.Count)
|
||||
break;
|
||||
for (int j = 0; j < ipie1.Indices.Count; j++)
|
||||
|
@ -739,12 +720,10 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
else
|
||||
break;
|
||||
}
|
||||
else if (prefix1 is IPropertyReferenceExpression)
|
||||
else if (prefix1 is IPropertyReferenceExpression ipre1)
|
||||
{
|
||||
IPropertyReferenceExpression ipre1 = (IPropertyReferenceExpression)prefix1;
|
||||
if (prefix2 is IPropertyReferenceExpression)
|
||||
if (prefix2 is IPropertyReferenceExpression ipre2)
|
||||
{
|
||||
IPropertyReferenceExpression ipre2 = (IPropertyReferenceExpression)prefix2;
|
||||
// we assume that mutating one property does not affect another.
|
||||
if (!ipre1.Property.Equals(ipre2.Property))
|
||||
break;
|
||||
|
@ -752,12 +731,10 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
else
|
||||
break;
|
||||
}
|
||||
else if (prefix1 is IFieldReferenceExpression)
|
||||
else if (prefix1 is IFieldReferenceExpression ifre1)
|
||||
{
|
||||
IFieldReferenceExpression ifre1 = (IFieldReferenceExpression)prefix1;
|
||||
if (prefix2 is IFieldReferenceExpression)
|
||||
if (prefix2 is IFieldReferenceExpression ifre2)
|
||||
{
|
||||
IFieldReferenceExpression ifre2 = (IFieldReferenceExpression)prefix2;
|
||||
if (!ifre1.Field.Equals(ifre2.Field))
|
||||
break;
|
||||
}
|
||||
|
@ -905,9 +882,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
{
|
||||
if (expr == null)
|
||||
return 0;
|
||||
else if (expr is IArrayIndexerExpression)
|
||||
else if (expr is IArrayIndexerExpression iaie)
|
||||
{
|
||||
IArrayIndexerExpression iaie = (IArrayIndexerExpression)expr;
|
||||
int maxDepth = GetExpressionDepth(iaie.Target);
|
||||
foreach (IExpression index in iaie.Indices)
|
||||
{
|
||||
|
@ -917,14 +893,12 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
}
|
||||
return 1 + maxDepth;
|
||||
}
|
||||
else if (expr is IMethodReferenceExpression)
|
||||
else if (expr is IMethodReferenceExpression imre)
|
||||
{
|
||||
IMethodReferenceExpression imre = (IMethodReferenceExpression)expr;
|
||||
return GetExpressionDepth(imre.Target);
|
||||
}
|
||||
else if (expr is IMethodInvokeExpression)
|
||||
else if (expr is IMethodInvokeExpression imie)
|
||||
{
|
||||
IMethodInvokeExpression imie = (IMethodInvokeExpression)expr;
|
||||
int maxDepth = GetExpressionDepth(imie.Method);
|
||||
foreach (IExpression arg in imie.Arguments)
|
||||
{
|
||||
|
@ -934,24 +908,20 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
}
|
||||
return 1 + maxDepth;
|
||||
}
|
||||
else if (expr is IBinaryExpression)
|
||||
else if (expr is IBinaryExpression ibe)
|
||||
{
|
||||
IBinaryExpression ibe = (IBinaryExpression)expr;
|
||||
return 1 + System.Math.Max(GetExpressionDepth(ibe.Left), GetExpressionDepth(ibe.Right));
|
||||
}
|
||||
else if (expr is IUnaryExpression)
|
||||
else if (expr is IUnaryExpression iue)
|
||||
{
|
||||
IUnaryExpression iue = (IUnaryExpression)expr;
|
||||
return 1 + GetExpressionDepth(iue.Expression);
|
||||
}
|
||||
else if (expr is IPropertyReferenceExpression)
|
||||
else if (expr is IPropertyReferenceExpression ipre)
|
||||
{
|
||||
IPropertyReferenceExpression ipre = (IPropertyReferenceExpression)expr;
|
||||
return 1 + GetExpressionDepth(ipre.Target);
|
||||
}
|
||||
else if (expr is IPropertyIndexerExpression)
|
||||
else if (expr is IPropertyIndexerExpression ipie)
|
||||
{
|
||||
IPropertyIndexerExpression ipie = (IPropertyIndexerExpression)expr;
|
||||
int maxDepth = GetExpressionDepth(ipie.Target);
|
||||
foreach (IExpression index in ipie.Indices)
|
||||
{
|
||||
|
@ -961,9 +931,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
}
|
||||
return 1 + maxDepth;
|
||||
}
|
||||
else if (expr is IObjectCreateExpression)
|
||||
else if (expr is IObjectCreateExpression ioce)
|
||||
{
|
||||
var ioce = (IObjectCreateExpression)expr;
|
||||
int maxDepth = GetExpressionDepth(ioce.Initializer);
|
||||
foreach (IExpression arg in ioce.Arguments)
|
||||
{
|
||||
|
@ -973,9 +942,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
}
|
||||
return 1 + maxDepth;
|
||||
}
|
||||
else if (expr is IAssignExpression)
|
||||
else if (expr is IAssignExpression iae)
|
||||
{
|
||||
IAssignExpression iae = (IAssignExpression)expr;
|
||||
return 1 + System.Math.Max(GetExpressionDepth(iae.Target), GetExpressionDepth(iae.Expression));
|
||||
}
|
||||
else if (expr is IVariableReferenceExpression || expr is ILiteralExpression || expr is IArgumentReferenceExpression || expr is ITypeReferenceExpression
|
||||
|
@ -1003,9 +971,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
|
||||
private IEnumerable<IExpression> GetSummands(IExpression expr)
|
||||
{
|
||||
if (expr is IBinaryExpression)
|
||||
if (expr is IBinaryExpression ibe)
|
||||
{
|
||||
IBinaryExpression ibe = (IBinaryExpression)expr;
|
||||
if (ibe.Operator == BinaryOperator.Add)
|
||||
{
|
||||
foreach (var summand in GetSummands(ibe.Left))
|
||||
|
@ -1013,7 +980,7 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
foreach (var summand in GetSummands(ibe.Right))
|
||||
yield return summand;
|
||||
}
|
||||
else if(ibe.Operator == BinaryOperator.Subtract)
|
||||
else if (ibe.Operator == BinaryOperator.Subtract)
|
||||
{
|
||||
foreach (var summand in GetSummands(ibe.Left))
|
||||
yield return summand;
|
||||
|
@ -1023,9 +990,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
else
|
||||
yield return expr;
|
||||
}
|
||||
else if (expr is IMethodInvokeExpression)
|
||||
else if (expr is IMethodInvokeExpression imie)
|
||||
{
|
||||
IMethodInvokeExpression imie = (IMethodInvokeExpression)expr;
|
||||
if (IsStaticMethod(imie, new Func<int, int, int>(ML.Probabilistic.Factors.Factor.Plus)))
|
||||
{
|
||||
foreach (var arg in imie.Arguments)
|
||||
|
@ -1050,18 +1016,16 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
|
||||
private IExpression Negate(IExpression expr)
|
||||
{
|
||||
if(expr is IMethodInvokeExpression)
|
||||
if (expr is IMethodInvokeExpression imie)
|
||||
{
|
||||
IMethodInvokeExpression imie = (IMethodInvokeExpression)expr;
|
||||
if (IsStaticMethod(imie, new Func<int, int>(ML.Probabilistic.Factors.Factor.Negate)))
|
||||
{
|
||||
return imie.Arguments[0];
|
||||
}
|
||||
}
|
||||
if(expr is IUnaryExpression)
|
||||
if (expr is IUnaryExpression iue)
|
||||
{
|
||||
IUnaryExpression iue = (IUnaryExpression)expr;
|
||||
if(iue.Operator == UnaryOperator.Negate)
|
||||
if (iue.Operator == UnaryOperator.Negate)
|
||||
{
|
||||
return iue.Expression;
|
||||
}
|
||||
|
@ -1080,10 +1044,9 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
IVariableDeclaration ivd = GetVariableDeclaration(expr);
|
||||
return bindings.TryGetValue(ivd, out value);
|
||||
}
|
||||
else if (expr is IBinaryExpression)
|
||||
else if (expr is IBinaryExpression ibe)
|
||||
{
|
||||
IBinaryExpression ibe = (IBinaryExpression)expr;
|
||||
if (ibe.Operator == BinaryOperator.ValueInequality ||
|
||||
if (ibe.Operator == BinaryOperator.ValueInequality ||
|
||||
ibe.Operator == BinaryOperator.IdentityInequality ||
|
||||
ibe.Operator == BinaryOperator.GreaterThan ||
|
||||
ibe.Operator == BinaryOperator.LessThan)
|
||||
|
@ -1140,9 +1103,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
return true;
|
||||
}
|
||||
}
|
||||
else if (expr is IUnaryExpression)
|
||||
else if (expr is IUnaryExpression iue)
|
||||
{
|
||||
IUnaryExpression iue = (IUnaryExpression)expr;
|
||||
T target;
|
||||
if (TryEvaluate(iue.Expression, bindings, out target))
|
||||
{
|
||||
|
@ -1152,9 +1114,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
return true;
|
||||
}
|
||||
}
|
||||
else if (expr is ILiteralExpression)
|
||||
else if (expr is ILiteralExpression ile)
|
||||
{
|
||||
ILiteralExpression ile = (ILiteralExpression)expr;
|
||||
if (ile.Value is T)
|
||||
{
|
||||
value = (T)ile.Value;
|
||||
|
@ -1247,19 +1208,17 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
|
||||
public IExpression LoopSizeExpression(IForStatement loop)
|
||||
{
|
||||
if (loop.Condition is IBinaryExpression)
|
||||
if (loop.Condition is IBinaryExpression ibe)
|
||||
{
|
||||
IBinaryExpression ibe = (IBinaryExpression)loop.Condition;
|
||||
if (ibe.Operator == BinaryOperator.LessThan)
|
||||
{
|
||||
return ibe.Right;
|
||||
}
|
||||
else if(ibe.Operator == BinaryOperator.GreaterThanOrEqual)
|
||||
else if (ibe.Operator == BinaryOperator.GreaterThanOrEqual)
|
||||
{
|
||||
var start = LoopStartExpression(loop);
|
||||
if(start is IBinaryExpression)
|
||||
if (start is IBinaryExpression ibe2)
|
||||
{
|
||||
IBinaryExpression ibe2 = (IBinaryExpression)start;
|
||||
if (ibe2.Operator == BinaryOperator.Subtract)
|
||||
{
|
||||
return ibe2.Left;
|
||||
|
@ -1281,9 +1240,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
|
||||
private IExpression LoopBreakExpression(IForStatement loop)
|
||||
{
|
||||
if (loop.Condition is IBinaryExpression)
|
||||
if (loop.Condition is IBinaryExpression ibe)
|
||||
{
|
||||
IBinaryExpression ibe = (IBinaryExpression)loop.Condition;
|
||||
if (ibe.Operator == BinaryOperator.LessThan)
|
||||
return Builder.BinaryExpr(BinaryOperator.Subtract, ibe.Right, Builder.LiteralExpr(1));
|
||||
else if (ibe.Operator == BinaryOperator.LessThanOrEqual)
|
||||
|
@ -1303,12 +1261,10 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
|
||||
public IExpression LoopStartExpression(IForStatement loop)
|
||||
{
|
||||
if (loop.Initializer is IExpressionStatement)
|
||||
if (loop.Initializer is IExpressionStatement ies)
|
||||
{
|
||||
IExpressionStatement ies = (IExpressionStatement)loop.Initializer;
|
||||
if (ies.Expression is IAssignExpression)
|
||||
if (ies.Expression is IAssignExpression iae)
|
||||
{
|
||||
IAssignExpression iae = (IAssignExpression)ies.Expression;
|
||||
return iae.Expression;
|
||||
}
|
||||
}
|
||||
|
@ -1332,9 +1288,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
|
||||
public void AddLoopBounds(Dictionary<IVariableDeclaration, Bounds> bounds, IStatement ist)
|
||||
{
|
||||
if (ist is IForStatement)
|
||||
if (ist is IForStatement ifs)
|
||||
{
|
||||
IForStatement ifs = (IForStatement)ist;
|
||||
IVariableDeclaration loopVar = LoopVariable(ifs);
|
||||
Bounds b;
|
||||
if (!bounds.TryGetValue(loopVar, out b))
|
||||
|
@ -1357,13 +1312,11 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
if (ifs.Body.Statements.Count == 1)
|
||||
AddLoopBounds(bounds, ifs.Body.Statements[0]);
|
||||
}
|
||||
else if (ist is IConditionStatement)
|
||||
else if (ist is IConditionStatement ics)
|
||||
{
|
||||
IConditionStatement ics = (IConditionStatement)ist;
|
||||
IExpression condition = ics.Condition;
|
||||
if (condition is IBinaryExpression)
|
||||
if (condition is IBinaryExpression ibe)
|
||||
{
|
||||
IBinaryExpression ibe = (IBinaryExpression)condition;
|
||||
if (ibe.Left is IVariableReferenceExpression)
|
||||
{
|
||||
IVariableDeclaration loopVar = GetVariableDeclaration(ibe.Left);
|
||||
|
@ -1476,9 +1429,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
Set<ConditionBinding> bindings = new Set<ConditionBinding>();
|
||||
while (true)
|
||||
{
|
||||
if (ist is IForStatement)
|
||||
if (ist is IForStatement ifs)
|
||||
{
|
||||
IForStatement ifs = (IForStatement)ist;
|
||||
IVariableDeclaration loopVar = LoopVariable(ifs);
|
||||
if (localVars != null)
|
||||
localVars.Add(loopVar);
|
||||
|
@ -1493,9 +1445,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
else
|
||||
break;
|
||||
}
|
||||
else if (ist is IConditionStatement)
|
||||
else if (ist is IConditionStatement ics)
|
||||
{
|
||||
IConditionStatement ics = (IConditionStatement)ist;
|
||||
bindings.Add(new ConditionBinding(ics.Condition));
|
||||
if (ics.Then.Statements.Count == 1)
|
||||
ist = ics.Then.Statements[0];
|
||||
|
@ -1535,8 +1486,7 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
|
||||
public IExpression StripIndexers(IExpression expr, bool varsOnly)
|
||||
{
|
||||
IArrayIndexerExpression iaie = expr as IArrayIndexerExpression;
|
||||
if (iaie == null)
|
||||
if (!(expr is IArrayIndexerExpression iaie))
|
||||
return expr;
|
||||
if ((!(iaie.Indices[0] is IVariableReferenceExpression)) && varsOnly)
|
||||
return expr;
|
||||
|
@ -1555,8 +1505,7 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
|
||||
public bool IsNewObject(IExpression expr, Type type)
|
||||
{
|
||||
IObjectCreateExpression ioce = expr as IObjectCreateExpression;
|
||||
if (ioce == null)
|
||||
if (!(expr is IObjectCreateExpression ioce))
|
||||
return false;
|
||||
Type t = Builder.ToType(ioce.Type);
|
||||
return t.Equals(type);
|
||||
|
@ -1724,10 +1673,6 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
ForEachPrefix(((IMethodInvokeExpression)expr).Method, action);
|
||||
else if (expr is IMethodReferenceExpression)
|
||||
ForEachPrefix(((IMethodReferenceExpression)expr).Target, action);
|
||||
else if (expr is IUnaryExpression)
|
||||
ForEachPrefix(((IUnaryExpression)expr).Expression, action);
|
||||
else if (expr is IAddressReferenceExpression)
|
||||
ForEachPrefix(((IAddressReferenceExpression)expr).Expression, action);
|
||||
else if (expr is IDelegateInvokeExpression)
|
||||
ForEachPrefix(((IDelegateInvokeExpression)expr).Target, action);
|
||||
action(expr);
|
||||
|
@ -1754,12 +1699,11 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
/// <returns></returns>
|
||||
public IEnumerable<object> GetVariablesAndParameters(IExpression expr)
|
||||
{
|
||||
if (expr is IArrayIndexerExpression)
|
||||
if (expr is IArrayIndexerExpression iaie)
|
||||
{
|
||||
IArrayIndexerExpression iaie = (IArrayIndexerExpression)expr;
|
||||
foreach (IExpression index in iaie.Indices)
|
||||
{
|
||||
foreach(var decl in GetVariablesAndParameters(index))
|
||||
foreach (var decl in GetVariablesAndParameters(index))
|
||||
{
|
||||
yield return decl;
|
||||
}
|
||||
|
@ -1767,48 +1711,42 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
foreach (var decl in GetVariablesAndParameters(iaie.Target))
|
||||
yield return decl;
|
||||
}
|
||||
else if (expr is IMethodInvokeExpression)
|
||||
else if (expr is IMethodInvokeExpression imie)
|
||||
{
|
||||
IMethodInvokeExpression imie = (IMethodInvokeExpression)expr;
|
||||
foreach (IExpression arg in imie.Arguments)
|
||||
{
|
||||
foreach (var decl in GetVariablesAndParameters(arg))
|
||||
yield return decl;
|
||||
}
|
||||
}
|
||||
else if (expr is IBinaryExpression)
|
||||
else if (expr is IBinaryExpression ibe)
|
||||
{
|
||||
IBinaryExpression ibe = (IBinaryExpression)expr;
|
||||
foreach (var decl in GetVariablesAndParameters(ibe.Left))
|
||||
yield return decl;
|
||||
foreach (var decl in GetVariablesAndParameters(ibe.Right))
|
||||
yield return decl;
|
||||
}
|
||||
else if (expr is IUnaryExpression)
|
||||
else if (expr is IUnaryExpression iue)
|
||||
{
|
||||
IUnaryExpression iue = (IUnaryExpression)expr;
|
||||
foreach (var decl in GetVariablesAndParameters(iue.Expression))
|
||||
yield return decl;
|
||||
}
|
||||
else if (expr is IPropertyIndexerExpression)
|
||||
else if (expr is IPropertyIndexerExpression ipie)
|
||||
{
|
||||
IPropertyIndexerExpression ipie = (IPropertyIndexerExpression)expr;
|
||||
foreach (IExpression index in ipie.Indices)
|
||||
foreach (var decl in GetVariablesAndParameters(index))
|
||||
yield return decl;
|
||||
foreach (var decl in GetVariablesAndParameters(ipie.Target))
|
||||
yield return decl;
|
||||
}
|
||||
else if (expr is IObjectCreateExpression)
|
||||
else if (expr is IObjectCreateExpression ioce)
|
||||
{
|
||||
var ioce = (IObjectCreateExpression)expr;
|
||||
foreach (IExpression arg in ioce.Arguments)
|
||||
foreach (var decl in GetVariablesAndParameters(arg))
|
||||
yield return decl;
|
||||
}
|
||||
else if (expr is IAssignExpression)
|
||||
else if (expr is IAssignExpression iae)
|
||||
{
|
||||
IAssignExpression iae = (IAssignExpression)expr;
|
||||
foreach (var decl in GetVariablesAndParameters(iae.Expression))
|
||||
yield return decl;
|
||||
foreach (var decl in GetVariablesAndParameters(iae.Target))
|
||||
|
@ -1826,23 +1764,21 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
{
|
||||
if (expr is IArgumentReferenceExpression)
|
||||
yield return (IArgumentReferenceExpression)expr;
|
||||
else if (expr is IArrayIndexerExpression)
|
||||
else if (expr is IArrayIndexerExpression iaie)
|
||||
{
|
||||
IArrayIndexerExpression iaie = (IArrayIndexerExpression)expr;
|
||||
foreach (IExpression index in iaie.Indices)
|
||||
foreach (var iare in GetArgumentReferenceExpressions(index))
|
||||
yield return iare;
|
||||
foreach (var iare in GetArgumentReferenceExpressions(iaie.Target))
|
||||
yield return iare;
|
||||
}
|
||||
else if (expr is IUnaryExpression)
|
||||
else if (expr is IUnaryExpression iue)
|
||||
{
|
||||
foreach (var iare in GetArgumentReferenceExpressions(((IUnaryExpression)expr).Expression))
|
||||
foreach (var iare in GetArgumentReferenceExpressions(iue.Expression))
|
||||
yield return iare;
|
||||
}
|
||||
else if (expr is IBinaryExpression)
|
||||
else if (expr is IBinaryExpression ibe)
|
||||
{
|
||||
IBinaryExpression ibe = (IBinaryExpression)expr;
|
||||
foreach (var iare in GetArgumentReferenceExpressions(ibe.Left))
|
||||
yield return iare;
|
||||
foreach (var iare in GetArgumentReferenceExpressions(ibe.Right))
|
||||
|
@ -1852,33 +1788,29 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
|
||||
public IEnumerable<IExpression> GetConditionAndTargetIndexExpressions(IStatement stmt)
|
||||
{
|
||||
if (stmt is IConditionStatement)
|
||||
if (stmt is IConditionStatement ics)
|
||||
{
|
||||
IConditionStatement ics = (IConditionStatement)stmt;
|
||||
yield return ics.Condition;
|
||||
foreach (var expr in GetConditionAndTargetIndexExpressions(ics.Then))
|
||||
yield return expr;
|
||||
}
|
||||
else if (stmt is IForStatement)
|
||||
else if (stmt is IForStatement ifs)
|
||||
{
|
||||
foreach (var expr in GetConditionAndTargetIndexExpressions(((IForStatement)stmt).Body))
|
||||
foreach (var expr in GetConditionAndTargetIndexExpressions(ifs.Body))
|
||||
yield return expr;
|
||||
}
|
||||
else if (stmt is IBlockStatement)
|
||||
else if (stmt is IBlockStatement ibs)
|
||||
{
|
||||
IBlockStatement ibs = (IBlockStatement)stmt;
|
||||
foreach (IStatement ist in ibs.Statements)
|
||||
{
|
||||
foreach (var expr in GetConditionAndTargetIndexExpressions(ist))
|
||||
yield return expr;
|
||||
}
|
||||
}
|
||||
else if (stmt is IExpressionStatement)
|
||||
else if (stmt is IExpressionStatement ies)
|
||||
{
|
||||
IExpressionStatement ies = (IExpressionStatement)stmt;
|
||||
if (ies.Expression is IAssignExpression)
|
||||
if (ies.Expression is IAssignExpression iae)
|
||||
{
|
||||
IAssignExpression iae = (IAssignExpression)ies.Expression;
|
||||
// target indices are considered "conditions" for this purpose
|
||||
foreach (var index in GetFlattenedIndices(iae.Target))
|
||||
yield return index;
|
||||
|
@ -1892,41 +1824,37 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
/// <param name="ist"></param>
|
||||
public IEnumerable<IExpression> GetTargets(IStatement ist)
|
||||
{
|
||||
if (ist is IExpressionStatement)
|
||||
if (ist is IExpressionStatement ies)
|
||||
{
|
||||
IExpressionStatement ies = (IExpressionStatement)ist;
|
||||
IExpression expr = ies.Expression;
|
||||
if (expr is IAssignExpression)
|
||||
{
|
||||
IAssignExpression iae = (IAssignExpression)ies.Expression;
|
||||
yield return iae.Target;
|
||||
yield return iae.Target;
|
||||
}
|
||||
else if (expr is IVariableDeclarationExpression)
|
||||
{
|
||||
yield return expr;
|
||||
}
|
||||
}
|
||||
else if (ist is IConditionStatement)
|
||||
else if (ist is IConditionStatement ics)
|
||||
{
|
||||
IConditionStatement ics = (IConditionStatement)ist;
|
||||
foreach (IStatement st in ics.Then.Statements)
|
||||
{
|
||||
foreach (var expr in GetTargets(st))
|
||||
yield return expr;
|
||||
}
|
||||
}
|
||||
else if (ist is IForStatement)
|
||||
else if (ist is IForStatement ifs)
|
||||
{
|
||||
IForStatement ifs = (IForStatement)ist;
|
||||
foreach (IStatement st in ifs.Body.Statements)
|
||||
{
|
||||
foreach (var expr in GetTargets(st))
|
||||
yield return expr;
|
||||
}
|
||||
}
|
||||
else if (ist is IWhileStatement)
|
||||
else if (ist is IWhileStatement iws)
|
||||
{
|
||||
IWhileStatement iws = (IWhileStatement)ist;
|
||||
foreach (IStatement st in iws.Body.Statements)
|
||||
{
|
||||
foreach (var expr in GetTargets(st))
|
||||
|
@ -1979,10 +1907,10 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
/// <returns></returns>
|
||||
public int GetIndexingDepth(IExpression iexpr)
|
||||
{
|
||||
IArrayIndexerExpression iaie = iexpr as IArrayIndexerExpression;
|
||||
if (iaie == null)
|
||||
if (iexpr is IArrayIndexerExpression iaie)
|
||||
return 1 + GetIndexingDepth(iaie.Target);
|
||||
else
|
||||
return 0;
|
||||
return 1 + GetIndexingDepth(iaie.Target);
|
||||
}
|
||||
|
||||
#if SUPPRESS_UNREACHABLE_CODE_WARNINGS
|
||||
|
@ -2003,9 +1931,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
return;
|
||||
if (expr is IArgumentReferenceExpression)
|
||||
return;
|
||||
if (expr is IArrayIndexerExpression)
|
||||
if (expr is IArrayIndexerExpression iaie)
|
||||
{
|
||||
IArrayIndexerExpression iaie = (IArrayIndexerExpression)expr;
|
||||
IVariableDeclaration[] vars = new IVariableDeclaration[iaie.Indices.Count];
|
||||
for (int i = 0; i < vars.Length; i++)
|
||||
{
|
||||
|
@ -2026,9 +1953,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
|
||||
private IEnumerable<IExpression> GetFlattenedIndices(IExpression expr)
|
||||
{
|
||||
if (expr is IArrayIndexerExpression)
|
||||
if (expr is IArrayIndexerExpression iaie)
|
||||
{
|
||||
IArrayIndexerExpression iaie = (IArrayIndexerExpression)expr;
|
||||
foreach (IExpression index in GetFlattenedIndices(iaie.Target))
|
||||
yield return index;
|
||||
foreach (IExpression index in iaie.Indices)
|
||||
|
@ -2068,9 +1994,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
/// <returns>The innermost expression</returns>
|
||||
private IExpression ForEachIndexingBracket(IExpression expr, Action<IList<IExpression>> action)
|
||||
{
|
||||
if (expr is IArrayIndexerExpression)
|
||||
if (expr is IArrayIndexerExpression iaie)
|
||||
{
|
||||
IArrayIndexerExpression iaie = (IArrayIndexerExpression)expr;
|
||||
var target = ForEachIndexingBracket(iaie.Target, action);
|
||||
action(iaie.Indices);
|
||||
return target;
|
||||
|
@ -2086,17 +2011,21 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
/// <returns></returns>
|
||||
public IExpression RemoveLastIndex(IExpression expr)
|
||||
{
|
||||
IArrayIndexerExpression iaie = expr as IArrayIndexerExpression;
|
||||
if (iaie == null)
|
||||
return expr;
|
||||
if (iaie.Target is IArrayIndexerExpression)
|
||||
if (expr is IArrayIndexerExpression iaie)
|
||||
{
|
||||
IArrayIndexerExpression aie = Builder.ArrayIndxrExpr();
|
||||
aie.Indices.AddRange(iaie.Indices);
|
||||
aie.Target = RemoveLastIndex(iaie.Target);
|
||||
return aie;
|
||||
if (iaie.Target is IArrayIndexerExpression)
|
||||
{
|
||||
IArrayIndexerExpression aie = Builder.ArrayIndxrExpr();
|
||||
aie.Indices.AddRange(iaie.Indices);
|
||||
aie.Target = RemoveLastIndex(iaie.Target);
|
||||
return aie;
|
||||
}
|
||||
else return iaie.Target;
|
||||
}
|
||||
else
|
||||
{
|
||||
return expr;
|
||||
}
|
||||
return iaie.Target;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -2116,9 +2045,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
{
|
||||
if (ti.inputElement == excludeAncestor)
|
||||
break;
|
||||
if (ti.inputElement is IForStatement)
|
||||
if (ti.inputElement is IForStatement loop)
|
||||
{
|
||||
IForStatement loop = (IForStatement)ti.inputElement;
|
||||
IVariableDeclaration loopVd = LoopVariable(loop);
|
||||
if (ivd.Name == loopVd.Name)
|
||||
return loop;
|
||||
|
@ -2129,38 +2057,32 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
|
||||
public bool IsForwardLoop(IForStatement ifs)
|
||||
{
|
||||
if (ifs.Increment is IExpressionStatement)
|
||||
if (ifs.Increment is IExpressionStatement ies)
|
||||
{
|
||||
IExpressionStatement ies = (IExpressionStatement)ifs.Increment;
|
||||
if (ies.Expression is IAssignExpression)
|
||||
if (ies.Expression is IAssignExpression iae)
|
||||
{
|
||||
IAssignExpression iae = (IAssignExpression)ies.Expression;
|
||||
IBinaryExpression ibe = ChannelTransform.RemoveCast(iae.Expression) as IBinaryExpression;
|
||||
if (ibe != null)
|
||||
if (ChannelTransform.RemoveCast(iae.Expression) is IBinaryExpression ibe)
|
||||
{
|
||||
if (ibe.Operator == BinaryOperator.Add)
|
||||
{
|
||||
if (ibe.Right is ILiteralExpression)
|
||||
if (ibe.Right is ILiteralExpression ile)
|
||||
{
|
||||
ILiteralExpression ile = (ILiteralExpression)ibe.Right;
|
||||
if (ile.Value is int)
|
||||
return ((int)ile.Value >= 0);
|
||||
}
|
||||
}
|
||||
else if (ibe.Operator == BinaryOperator.Subtract)
|
||||
{
|
||||
if (ibe.Right is ILiteralExpression)
|
||||
if (ibe.Right is ILiteralExpression ile)
|
||||
{
|
||||
ILiteralExpression ile = (ILiteralExpression)ibe.Right;
|
||||
if (ile.Value is int)
|
||||
return ((int)ile.Value < 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (ies.Expression is IUnaryExpression)
|
||||
else if (ies.Expression is IUnaryExpression iue)
|
||||
{
|
||||
IUnaryExpression iue = (IUnaryExpression)ies.Expression;
|
||||
return (iue.Operator == UnaryOperator.PostIncrement) || (iue.Operator == UnaryOperator.PreIncrement);
|
||||
}
|
||||
}
|
||||
|
@ -2352,9 +2274,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
{
|
||||
if (expr is ILiteralExpression) return false;
|
||||
if (expr is IDefaultExpression) return false;
|
||||
if (expr is IMethodInvokeExpression)
|
||||
if (expr is IMethodInvokeExpression imie)
|
||||
{
|
||||
IMethodInvokeExpression imie = (IMethodInvokeExpression)expr;
|
||||
bool stochArgs = IsAnyStochastic(context, imie.Arguments);
|
||||
bool stochFactor = false;
|
||||
FactorManager.FactorInfo info = GetFactorInfo(context, imie);
|
||||
|
@ -2367,31 +2288,26 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
if (expr is IPropertyReferenceExpression) return IsStochastic(context, ((IPropertyReferenceExpression)expr).Target);
|
||||
if (expr is IFieldReferenceExpression) return IsStochastic(context, ((IFieldReferenceExpression)expr).Target);
|
||||
if (expr is IArrayCreateExpression) return false;
|
||||
if (expr is IObjectCreateExpression)
|
||||
if (expr is IObjectCreateExpression ioce)
|
||||
{
|
||||
IObjectCreateExpression ioce = (IObjectCreateExpression)expr;
|
||||
return IsAnyStochastic(context, ioce.Arguments);
|
||||
}
|
||||
if (expr is IUnaryExpression)
|
||||
if (expr is IUnaryExpression iue)
|
||||
{
|
||||
IUnaryExpression iue = (IUnaryExpression)expr;
|
||||
return IsStochastic(context, iue.Expression);
|
||||
}
|
||||
if (expr is IBinaryExpression)
|
||||
if (expr is IBinaryExpression ibe)
|
||||
{
|
||||
IBinaryExpression ibe = (IBinaryExpression)expr;
|
||||
return IsStochastic(context, ibe.Left) || IsStochastic(context, ibe.Right);
|
||||
}
|
||||
if (expr is IArrayIndexerExpression)
|
||||
if (expr is IArrayIndexerExpression iaie)
|
||||
{
|
||||
IArrayIndexerExpression iaie = (IArrayIndexerExpression)expr;
|
||||
return IsStochastic(context, iaie.Target) || IsAnyStochastic(context, iaie.Indices);
|
||||
}
|
||||
if (expr is ICastExpression) return IsStochastic(context, ((ICastExpression)expr).Expression);
|
||||
if (expr is ICheckedExpression) return IsStochastic(context, ((ICheckedExpression)expr).Expression);
|
||||
if (expr is IPropertyIndexerExpression)
|
||||
if (expr is IPropertyIndexerExpression ipie)
|
||||
{
|
||||
IPropertyIndexerExpression ipie = (IPropertyIndexerExpression)expr;
|
||||
return IsStochastic(context, ipie.Target) || IsAnyStochastic(context, ipie.Indices);
|
||||
}
|
||||
if (expr is IAddressDereferenceExpression) return false;
|
||||
|
@ -2443,9 +2359,8 @@ namespace Microsoft.ML.Probabilistic.Compiler
|
|||
internal static IExpression RemoveCast(IExpression expr)
|
||||
{
|
||||
// used to remove spurious casts
|
||||
if (expr is ICastExpression)
|
||||
if (expr is ICastExpression ice)
|
||||
{
|
||||
ICastExpression ice = (ICastExpression)expr;
|
||||
if (expr.GetExpressionType().IsAssignableFrom(ice.Expression.GetExpressionType()))
|
||||
{
|
||||
return ice.Expression;
|
||||
|
|
|
@ -71,7 +71,7 @@ namespace LDAExample
|
|||
// Draw the number of unique topics in the doc.
|
||||
int numUniqueTopicsPerDoc = Math.Min(1 + Poisson.Sample(1.0), numTopics);
|
||||
double expectedRepeatOfTopicInDoc =
|
||||
averageDocLength / numUniqueTopicsPerDoc;
|
||||
(double)averageDocLength / numUniqueTopicsPerDoc;
|
||||
int[] shuffledTopicIndices = Rand.Perm(numTopics);
|
||||
for (int j = 0; j < numUniqueTopicsPerDoc; j++)
|
||||
{
|
||||
|
|
|
@ -631,11 +631,7 @@ namespace Microsoft.ML.Probabilistic.Learners.BayesPointMachineClassifierInterna
|
|||
/// <param name="iterationChangedEventArgs">The information describing the change in iterations.</param>
|
||||
private void OnIterationChanged(object sender, BayesPointMachineClassifierIterationChangedEventArgs iterationChangedEventArgs)
|
||||
{
|
||||
EventHandler<BayesPointMachineClassifierIterationChangedEventArgs> handler = this.IterationChanged;
|
||||
if (handler != null)
|
||||
{
|
||||
handler(this, iterationChangedEventArgs);
|
||||
}
|
||||
this.IterationChanged?.Invoke(this, iterationChangedEventArgs);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -645,11 +641,7 @@ namespace Microsoft.ML.Probabilistic.Learners.BayesPointMachineClassifierInterna
|
|||
private void OnBatchedIterationChanged(int completedIteration)
|
||||
{
|
||||
// Raise IterationChanged event
|
||||
EventHandler<BayesPointMachineClassifierIterationChangedEventArgs> handler = this.IterationChanged;
|
||||
if (handler != null)
|
||||
{
|
||||
handler(this, new BayesPointMachineClassifierIterationChangedEventArgs(completedIteration, this.InferenceAlgorithms.WeightDistributions));
|
||||
}
|
||||
this.IterationChanged?.Invoke(this, new BayesPointMachineClassifierIterationChangedEventArgs(completedIteration, this.InferenceAlgorithms.WeightDistributions));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
|
|
@ -376,11 +376,7 @@ namespace Microsoft.ML.Probabilistic.Learners.BayesPointMachineClassifierInterna
|
|||
/// <param name="iterationChangedEventArgs">The information describing the change in iterations.</param>
|
||||
private void OnIterationChanged(object sender, BayesPointMachineClassifierIterationChangedEventArgs iterationChangedEventArgs)
|
||||
{
|
||||
EventHandler<BayesPointMachineClassifierIterationChangedEventArgs> handler = this.IterationChanged;
|
||||
if (handler != null)
|
||||
{
|
||||
handler(this, iterationChangedEventArgs);
|
||||
}
|
||||
this.IterationChanged?.Invoke(this, iterationChangedEventArgs);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
|
|
@ -42,7 +42,7 @@ namespace Microsoft.ML.Probabilistic.Learners.MatchboxRecommenderInternal
|
|||
/// <param name="reader">The binary reader to read the distribution over feature weights from.</param>
|
||||
public FeatureParameterDistribution(BinaryReader reader)
|
||||
{
|
||||
Debug.Assert(reader != null, "The reader must not be null.");
|
||||
if (reader == null) throw new ArgumentNullException(nameof(reader));
|
||||
|
||||
int deserializedVersion = reader.ReadSerializationVersion(CustomSerializationVersion);
|
||||
|
||||
|
@ -60,11 +60,18 @@ namespace Microsoft.ML.Probabilistic.Learners.MatchboxRecommenderInternal
|
|||
/// <param name="biasFeatureWeightDistribution">The distribution over weights of the feature contribution to biases.</param>
|
||||
public FeatureParameterDistribution(GaussianMatrix traitFeatureWeightDistribution, GaussianArray biasFeatureWeightDistribution)
|
||||
{
|
||||
Debug.Assert(
|
||||
(traitFeatureWeightDistribution == null && biasFeatureWeightDistribution == null) ||
|
||||
traitFeatureWeightDistribution.All(w => w != null && w.Count == biasFeatureWeightDistribution.Count),
|
||||
"The provided distributions should be valid and consistent in the number of features.");
|
||||
|
||||
if (traitFeatureWeightDistribution == null)
|
||||
traitFeatureWeightDistribution = new GaussianMatrix(0);
|
||||
if (biasFeatureWeightDistribution == null)
|
||||
biasFeatureWeightDistribution = new GaussianArray(0);
|
||||
foreach (var w in traitFeatureWeightDistribution)
|
||||
{
|
||||
if (w == null)
|
||||
throw new ArgumentException(nameof(traitFeatureWeightDistribution), "Element is null");
|
||||
if (w.Count != biasFeatureWeightDistribution.Count)
|
||||
throw new ArgumentException(nameof(traitFeatureWeightDistribution), "Feature count does not match biasFeatureWeightDistribution.Count");
|
||||
}
|
||||
|
||||
this.TraitWeights = traitFeatureWeightDistribution;
|
||||
this.BiasWeights = biasFeatureWeightDistribution;
|
||||
}
|
||||
|
|
|
@ -453,21 +453,13 @@ namespace Microsoft.ML.Probabilistic.Learners.Runners
|
|||
for (int c = 0; c < classLabelCount; c++)
|
||||
{
|
||||
// ...find the longest string among counts and label
|
||||
int labelWidth = labels[c].Length;
|
||||
int labelWidth = Math.Min(labels[c].Length, MaxLabelWidth);
|
||||
|
||||
columnWidths[c + 1] = labelWidth > MaxLabelWidth ? MaxLabelWidth : labelWidth;
|
||||
for (int r = 0; r < classLabelCount; r++)
|
||||
{
|
||||
int countWidth = MaxValueWidth;
|
||||
if (countWidth > columnWidths[c + 1])
|
||||
{
|
||||
columnWidths[c + 1] = countWidth;
|
||||
}
|
||||
}
|
||||
columnWidths[c + 1] = Math.Max(MaxValueWidth, labelWidth);
|
||||
|
||||
if (labelWidth > columnWidths[0])
|
||||
{
|
||||
columnWidths[0] = labelWidth > MaxLabelWidth ? MaxLabelWidth : labelWidth;
|
||||
columnWidths[0] = labelWidth;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -239,9 +239,9 @@ namespace Microsoft.ML.Probabilistic.Learners.Runners.MovieLens
|
|||
public static Tuple<int, string> ComputeMovieGenre(int offset, string feature)
|
||||
{
|
||||
string[] genres = feature.Split('|');
|
||||
if (genres.Length < 1 && genres.Length > 3)
|
||||
if (genres.Length < 1 || genres.Length > 3)
|
||||
{
|
||||
throw new ArgumentException(string.Format("Movies should have between 1 and 3 genres; given {0}.", genres.Length));
|
||||
throw new ArgumentOutOfRangeException(nameof(feature), feature, "Movies should have between 1 and 3 genres.");
|
||||
}
|
||||
|
||||
double value = 1.0 / genres.Length;
|
||||
|
|
|
@ -110,10 +110,7 @@ namespace Microsoft.ML.Probabilistic.Learners.Runners
|
|||
public void Execute()
|
||||
{
|
||||
// Report that the run has been started
|
||||
if (this.Started != null)
|
||||
{
|
||||
this.Started(this, EventArgs.Empty);
|
||||
}
|
||||
this.Started?.Invoke(this, EventArgs.Empty);
|
||||
|
||||
try
|
||||
{
|
||||
|
@ -166,12 +163,8 @@ namespace Microsoft.ML.Probabilistic.Learners.Runners
|
|||
TimeSpan totalFoldTime = totalFoldTimer.Elapsed;
|
||||
|
||||
// Report that the fold has been processed
|
||||
if (this.FoldProcessed != null)
|
||||
{
|
||||
this.FoldProcessed(
|
||||
this,
|
||||
new RecommenderRunFoldProcessedEventArgs(i, totalFoldTime, foldTrainingTime, foldPredictionTime, foldEvaluationTime, foldMetrics));
|
||||
}
|
||||
this.FoldProcessed?.Invoke(this,
|
||||
new RecommenderRunFoldProcessedEventArgs(i, totalFoldTime, foldTrainingTime, foldPredictionTime, foldEvaluationTime, foldMetrics));
|
||||
|
||||
// Merge the timings
|
||||
totalTrainingTime += foldTrainingTime;
|
||||
|
@ -191,19 +184,12 @@ namespace Microsoft.ML.Probabilistic.Learners.Runners
|
|||
|
||||
// Report that the run has been completed
|
||||
TimeSpan totalTime = totalTimer.Elapsed;
|
||||
if (this.Completed != null)
|
||||
{
|
||||
this.Completed(
|
||||
this,
|
||||
new RecommenderRunCompletedEventArgs(totalTime, totalTrainingTime, totalPredictionTime, totalEvaluationTime, metrics));
|
||||
}
|
||||
this.Completed?.Invoke(this,
|
||||
new RecommenderRunCompletedEventArgs(totalTime, totalTrainingTime, totalPredictionTime, totalEvaluationTime, metrics));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (this.Interrupted != null)
|
||||
{
|
||||
this.Interrupted(this, new RecommenderRunInterruptedEventArgs(e));
|
||||
}
|
||||
this.Interrupted?.Invoke(this, new RecommenderRunInterruptedEventArgs(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -185,8 +185,7 @@ namespace Microsoft.ML.Probabilistic.Collections
|
|||
/// <param name="i">Position in the Items array</param>
|
||||
public void RemoveAt(int i)
|
||||
{
|
||||
if (Moved != null)
|
||||
Moved(Items[i], -1);
|
||||
Moved?.Invoke(Items[i], -1);
|
||||
Items[i] = Items[Count - 1];
|
||||
Items.RemoveAt(Count - 1);
|
||||
if (i < Count) SiftDown(i);
|
||||
|
@ -224,10 +223,10 @@ namespace Microsoft.ML.Probabilistic.Collections
|
|||
i = Parent(i))
|
||||
{
|
||||
Items[i] = Items[Parent(i)];
|
||||
if (Moved != null) Moved(Items[i], i);
|
||||
Moved?.Invoke(Items[i], i);
|
||||
}
|
||||
Items[i] = item;
|
||||
if (Moved != null) Moved(Items[i], i);
|
||||
Moved?.Invoke(Items[i], i);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -259,8 +258,7 @@ namespace Microsoft.ML.Probabilistic.Collections
|
|||
Items[smallest] = item;
|
||||
SiftDown(smallest);
|
||||
}
|
||||
// this can be simplified in C# 6
|
||||
if (Moved != null) Moved(Items[i], i);
|
||||
Moved?.Invoke(Items[i], i);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -476,13 +476,7 @@ namespace Microsoft.ML.Probabilistic.Math
|
|||
/// <param name="rmsDeriv"></param>
|
||||
protected internal void RaiseIterationEvent(int iteration, double objVal, double rmsDeriv)
|
||||
{
|
||||
// Safely invoke an event:
|
||||
IterationEventHandler temp = OnIteration;
|
||||
|
||||
if (temp != null)
|
||||
{
|
||||
temp(this, new OptimiserIterationEventArgs(iteration, objVal, rmsDeriv));
|
||||
}
|
||||
OnIteration?.Invoke(this, new OptimiserIterationEventArgs(iteration, objVal, rmsDeriv));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -423,41 +423,41 @@ namespace Microsoft.ML.Probabilistic.Math
|
|||
List<double> aTerm = new List<double>();
|
||||
List<double> cTerm = new List<double>();
|
||||
List<double> eTerm = new List<double>();
|
||||
Func<int, double> aFunc = n =>
|
||||
{
|
||||
double neg = (n % 2) == 0 ? 1.0 : -1.0;
|
||||
return (a <= b) ?
|
||||
2.0 * q * (1.0 + neg * Math.Pow(a / b, n + 1)) / (2.0 + n) :
|
||||
2.0 * p * (neg + Math.Pow(b / a, n + 1)) / (2.0 + n);
|
||||
};
|
||||
double aFunc(int n)
|
||||
{
|
||||
double neg = (n % 2) == 0 ? 1.0 : -1.0;
|
||||
return (a <= b) ?
|
||||
2.0 * q * (1.0 + neg * Math.Pow(a / b, n + 1)) / (2.0 + n) :
|
||||
2.0 * p * (neg + Math.Pow(b / a, n + 1)) / (2.0 + n);
|
||||
}
|
||||
|
||||
// Assumes aTerm has been populated up to n
|
||||
Func<int, double, double> bFunc = (n, r) =>
|
||||
double bFunc(int n, double r)
|
||||
{
|
||||
if (n == 0)
|
||||
{
|
||||
if (n == 0)
|
||||
return 1.0;
|
||||
}
|
||||
else if (n == 1)
|
||||
{
|
||||
return r * aTerm[1];
|
||||
}
|
||||
else
|
||||
{
|
||||
List<double> bTerm = new List<double>();
|
||||
bTerm.Add(1.0);
|
||||
bTerm.Add(r * aTerm[1]);
|
||||
for (int j = 2; j <= n; j++)
|
||||
{
|
||||
return 1.0;
|
||||
bTerm.Add(r * aTerm[j] + Enumerable.Range(1, j - 1).Sum(i => ((j - i) * r - i) * bTerm[i] * aTerm[j - i]) / j);
|
||||
}
|
||||
else if (n == 1)
|
||||
{
|
||||
return r * aTerm[1];
|
||||
}
|
||||
else
|
||||
{
|
||||
List<double> bTerm = new List<double>();
|
||||
bTerm.Add(1.0);
|
||||
bTerm.Add(r * aTerm[1]);
|
||||
for (int j = 2; j <= n; j++)
|
||||
{
|
||||
bTerm.Add(r * aTerm[j] + Enumerable.Range(1, j - 1).Sum(i => ((j - i) * r - i) * bTerm[i] * aTerm[j - i]) / j);
|
||||
}
|
||||
|
||||
return bTerm[n];
|
||||
}
|
||||
};
|
||||
return bTerm[n];
|
||||
}
|
||||
}
|
||||
|
||||
// Assumes aTerm has been populated up to n
|
||||
Func<int, double> cFunc = n => bFunc(n - 1, -n / 2.0) / n;
|
||||
double cFunc(int n) => bFunc(n - 1, -n / 2.0) / n;
|
||||
aTerm.Add(aFunc(0));
|
||||
aTerm.Add(aFunc(1));
|
||||
cTerm.Add(cFunc(1));
|
||||
|
@ -1826,7 +1826,7 @@ f = 1/gamma(x+1)-1
|
|||
|
||||
// [0] contains moments for x=-2
|
||||
// [1] contains moments for x=-3, etc.
|
||||
private static double[][] NormalCdfMomentRatioTable = new double[7][];
|
||||
private static readonly double[][] NormalCdfMomentRatioTable = new double[7][];
|
||||
|
||||
/// <summary>
|
||||
/// Computes int_0^infinity t^n N(t;x,1) dt / (n! N(x;0,1))
|
||||
|
@ -3018,7 +3018,7 @@ f = 1/gamma(x+1)-1
|
|||
}
|
||||
|
||||
// Integrate using quadrature nodes and weights
|
||||
private static double integrate(Converter<double, double> f, Vector nodes, Vector weights)
|
||||
private static double Integrate(Converter<double, double> f, Vector nodes, Vector weights)
|
||||
{
|
||||
return weights.Inner(nodes, x => f(x));
|
||||
}
|
||||
|
@ -3104,10 +3104,10 @@ f = 1/gamma(x+1)-1
|
|||
|
||||
if (variance > LogisticGaussianVarianceThreshold)
|
||||
{
|
||||
Converter<double, double> f = delegate (double x)
|
||||
double f(double x)
|
||||
{
|
||||
return Math.Exp(MMath.LogisticLn(x) + Gaussian.GetLogProb(x, mean, variance));
|
||||
};
|
||||
}
|
||||
double upperBound = mean + Math.Sqrt(variance);
|
||||
upperBound = Math.Max(upperBound, 10);
|
||||
return Quadrature.AdaptiveClenshawCurtis(f, upperBound, 32, 1e-10);
|
||||
|
@ -3119,12 +3119,11 @@ f = 1/gamma(x+1)-1
|
|||
double m_p, v_p;
|
||||
BigvProposal(mean, variance, out m_p, out v_p);
|
||||
Quadrature.GaussianNodesAndWeights(m_p, v_p, nodes, weights);
|
||||
Converter<double, double> weightedIntegrand =
|
||||
delegate (double z)
|
||||
{
|
||||
return Math.Exp(MMath.LogisticLn(z) + Gaussian.GetLogProb(z, mean, variance) - Gaussian.GetLogProb(z, m_p, v_p));
|
||||
};
|
||||
return integrate(weightedIntegrand, nodes, weights);
|
||||
double weightedIntegrand(double z)
|
||||
{
|
||||
return Math.Exp(MMath.LogisticLn(z) + Gaussian.GetLogProb(z, mean, variance) - Gaussian.GetLogProb(z, m_p, v_p));
|
||||
}
|
||||
return Integrate(weightedIntegrand, nodes, weights);
|
||||
}
|
||||
/*
|
||||
else {
|
||||
|
@ -3214,10 +3213,10 @@ else if (m < 20.0 - 60.0/11.0 * s) {
|
|||
|
||||
if (variance > LogisticGaussianVarianceThreshold)
|
||||
{
|
||||
Converter<double, double> f = delegate (double x)
|
||||
double f(double x)
|
||||
{
|
||||
return Math.Exp(MMath.LogisticLn(x) + MMath.LogisticLn(-x) + Gaussian.GetLogProb(x, mean, variance));
|
||||
};
|
||||
}
|
||||
return Quadrature.AdaptiveClenshawCurtis(f, 10, 32, 1e-10);
|
||||
}
|
||||
else
|
||||
|
@ -3227,12 +3226,11 @@ else if (m < 20.0 - 60.0/11.0 * s) {
|
|||
double m_p, v_p;
|
||||
BigvProposal(mean, variance, out m_p, out v_p);
|
||||
Quadrature.GaussianNodesAndWeights(m_p, v_p, nodes, weights);
|
||||
Converter<double, double> weightedIntegrand =
|
||||
delegate (double z)
|
||||
{
|
||||
return Math.Exp(MMath.LogisticLn(z) + MMath.LogisticLn(-z) + Gaussian.GetLogProb(z, mean, variance) - Gaussian.GetLogProb(z, m_p, v_p));
|
||||
};
|
||||
return integrate(weightedIntegrand, nodes, weights);
|
||||
double weightedIntegrand(double z)
|
||||
{
|
||||
return Math.Exp(MMath.LogisticLn(z) + MMath.LogisticLn(-z) + Gaussian.GetLogProb(z, mean, variance) - Gaussian.GetLogProb(z, m_p, v_p));
|
||||
}
|
||||
return Integrate(weightedIntegrand, nodes, weights);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -3322,13 +3320,13 @@ else if (m < 20.0 - 60.0/11.0 * s) {
|
|||
|
||||
if (variance > LogisticGaussianVarianceThreshold)
|
||||
{
|
||||
Converter<double, double> f = delegate (double x)
|
||||
{
|
||||
double logSigma = MMath.LogisticLn(x);
|
||||
double log1MinusSigma = MMath.LogisticLn(-x);
|
||||
double OneMinus2Sigma = -Math.Tanh(x / 2);
|
||||
return OneMinus2Sigma * Math.Exp(logSigma + log1MinusSigma + Gaussian.GetLogProb(x, mean, variance));
|
||||
};
|
||||
double f(double x)
|
||||
{
|
||||
double logSigma = MMath.LogisticLn(x);
|
||||
double log1MinusSigma = MMath.LogisticLn(-x);
|
||||
double OneMinus2Sigma = -Math.Tanh(x / 2);
|
||||
return OneMinus2Sigma * Math.Exp(logSigma + log1MinusSigma + Gaussian.GetLogProb(x, mean, variance));
|
||||
}
|
||||
return Quadrature.AdaptiveClenshawCurtis(f, 10, 32, 1e-10);
|
||||
}
|
||||
else
|
||||
|
@ -3338,14 +3336,14 @@ else if (m < 20.0 - 60.0/11.0 * s) {
|
|||
double m_p, v_p;
|
||||
BigvProposal(mean, variance, out m_p, out v_p);
|
||||
Quadrature.GaussianNodesAndWeights(m_p, v_p, nodes, weights);
|
||||
Converter<double, double> weightedIntegrand = delegate (double z)
|
||||
{
|
||||
double logSigma = MMath.LogisticLn(z);
|
||||
double log1MinusSigma = MMath.LogisticLn(-z);
|
||||
double OneMinus2Sigma = -Math.Tanh(z / 2);
|
||||
return OneMinus2Sigma * Math.Exp(logSigma + log1MinusSigma + Gaussian.GetLogProb(z, mean, variance) - Gaussian.GetLogProb(z, m_p, v_p));
|
||||
};
|
||||
return integrate(weightedIntegrand, nodes, weights);
|
||||
double weightedIntegrand(double z)
|
||||
{
|
||||
double logSigma = MMath.LogisticLn(z);
|
||||
double log1MinusSigma = MMath.LogisticLn(-z);
|
||||
double OneMinus2Sigma = -Math.Tanh(z / 2);
|
||||
return OneMinus2Sigma * Math.Exp(logSigma + log1MinusSigma + Gaussian.GetLogProb(z, mean, variance) - Gaussian.GetLogProb(z, m_p, v_p));
|
||||
}
|
||||
return Integrate(weightedIntegrand, nodes, weights);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -3381,7 +3379,7 @@ else if (m < 20.0 - 60.0/11.0 * s) {
|
|||
if (k < 0 || k > 2) throw new ArgumentException("invalid k (" + k + ")");
|
||||
double a = mean / variance;
|
||||
// int 0.5 cosh(x(m/v+1/2))/cosh(x/2) N(x;0,v) dx
|
||||
Converter<double, double> f = delegate (double x)
|
||||
double f(double x)
|
||||
{
|
||||
double logSigma = MMath.LogisticLn(x);
|
||||
double extra = 0;
|
||||
|
@ -3389,7 +3387,7 @@ else if (m < 20.0 - 60.0/11.0 * s) {
|
|||
if (k > 0) extra += MMath.LogisticLn(-x);
|
||||
if (k > 1) s = -Math.Tanh(x / 2);
|
||||
return s * Math.Exp(logSigma + extra + x * a + Gaussian.GetLogProb(x, 0, variance));
|
||||
};
|
||||
}
|
||||
double upperBound = (Math.Abs(a + 0.5) - 0.5) * variance + Math.Sqrt(variance);
|
||||
upperBound = Math.Max(upperBound, 10);
|
||||
return Quadrature.AdaptiveClenshawCurtis(f, upperBound, 32, 1e-10);
|
||||
|
|
|
@ -42,6 +42,7 @@ namespace Microsoft.ML.Probabilistic.Distributions.Automata
|
|||
this.IgnoreElementDistributionDetails = ignoreElementDistributionDetails;
|
||||
this.TruncationLength = truncationLength;
|
||||
this.EscapeCharacters = escapeCharacters;
|
||||
this.UseLazyQuantifier = useLazyQuantifier;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -1124,7 +1124,7 @@ namespace Microsoft.ML.Probabilistic.Distributions.Automata
|
|||
/// <summary>
|
||||
/// A static instance of an interval comparer.
|
||||
/// </summary>
|
||||
private static IComparer<IntervalTreeNode> intervalComparerInstance = new IntervalNodeComparer();
|
||||
private static readonly IComparer<IntervalTreeNode> intervalComparerInstance = new IntervalNodeComparer();
|
||||
|
||||
/// <summary>
|
||||
/// Backing field for <see cref="Children"/>.
|
||||
|
|
|
@ -107,8 +107,8 @@ namespace Microsoft.ML.Probabilistic.Distributions
|
|||
{
|
||||
if (probability < 0) throw new ArgumentOutOfRangeException(nameof(probability), "probability < 0");
|
||||
if (probability > 1.0) throw new ArgumentOutOfRangeException(nameof(probability), "probability > 1.0");
|
||||
int n = quantiles.Length;
|
||||
if (quantiles == null) throw new ArgumentNullException(nameof(quantiles));
|
||||
int n = quantiles.Length;
|
||||
if (n == 0) throw new ArgumentException("quantiles array is empty", nameof(quantiles));
|
||||
if (probability == 1.0)
|
||||
{
|
||||
|
|
|
@ -71,7 +71,7 @@ namespace Microsoft.ML.Probabilistic.Distributions
|
|||
/// 1 = interpolate (i/n + (i+1)/n)/2 = (i+0.5)/n
|
||||
/// 2 = interpolate i/(n-1)
|
||||
/// </summary>
|
||||
private static int InterpolationType = 0;
|
||||
private readonly int InterpolationType = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new QuantileEstimator.
|
||||
|
|
|
@ -294,16 +294,17 @@ namespace Microsoft.ML.Probabilistic.Factors
|
|||
double v = xv + mv;
|
||||
double lowerBound = 0;
|
||||
double upperBound = Double.PositiveInfinity;
|
||||
bool precisionIsBetween = true;
|
||||
bool precisionIsBetween;
|
||||
if (mean.Precision >= 0)
|
||||
{
|
||||
if (sample.Precision < -mean.Precision)
|
||||
throw new ImproperMessageException(sample);
|
||||
precisionIsBetween = true;
|
||||
//lowerBound = -mean.Precision * sample.Precision / (mean.Precision + sample.Precision);
|
||||
lowerBound = -1.0 / v;
|
||||
}
|
||||
else
|
||||
{ // mean.Precision < 0
|
||||
else // mean.Precision < 0
|
||||
{
|
||||
if (sample.Precision < 0)
|
||||
{
|
||||
precisionIsBetween = true;
|
||||
|
@ -316,12 +317,18 @@ namespace Microsoft.ML.Probabilistic.Factors
|
|||
lowerBound = 0;
|
||||
upperBound = -mean.Precision;
|
||||
}
|
||||
else
|
||||
else // sample.Precision >= -mean.Precision > 0
|
||||
{
|
||||
// mv < v < 0
|
||||
// 0 < -v < -mv
|
||||
// we want 1/(mv + 1/prec) > -sample.Precision
|
||||
// If mv + 1/prec > 0 (1/prec > -mv) then -xv < mv + 1/prec, 1/prec > -v, prec < -1/v, prec < -1/mv (latter is stronger)
|
||||
// If mv + 1/prec < 0 (1/prec < -mv) then -xv > mv + 1/prec, 1/prec < -v, prec > -1/v, prec > -1/mv (former is stronger)
|
||||
// Therefore the integration region is (prec < -1/mv) and (prec > -1/v).
|
||||
// in this case, the precision should NOT be in this interval.
|
||||
precisionIsBetween = false;
|
||||
lowerBound = -mean.Precision;
|
||||
lowerBound = -1.0 / v;
|
||||
upperBound = -1.0 / v;
|
||||
}
|
||||
}
|
||||
double[] nodes = new double[QuadratureNodeCount];
|
||||
|
@ -1468,27 +1475,28 @@ namespace Microsoft.ML.Probabilistic.Factors
|
|||
//Console.WriteLine("inflection points = {0}", StringUtil.CollectionToString(inflectionPoints, " "));
|
||||
inflectionPoints = inflectionPoints.ConvertAll(x => Math.Log(x));
|
||||
// find the maximum and the target value
|
||||
Func<double, double> like = logx =>
|
||||
double like(double logx)
|
||||
{
|
||||
if (double.IsInfinity(logx))
|
||||
return double.NegativeInfinity;
|
||||
double x = Math.Exp(logx);
|
||||
double vx = v + 1/x;
|
||||
double vx = v + 1 / x;
|
||||
return a * logx - b * x - 0.5 * Math.Log(vx) - 0.5 * m2 / vx;
|
||||
};
|
||||
}
|
||||
var stationaryValues = stationaryPoints.ConvertAll(logx => like(logx));
|
||||
double max = MMath.Max(stationaryValues);
|
||||
double logx0 = stationaryPoints[stationaryValues.IndexOf(max)];
|
||||
logrmode = logx0;
|
||||
Func<double,double> func = logx =>
|
||||
double func(double logx)
|
||||
{
|
||||
return LogLikelihoodRatio(logx, logx0, m, v, a, b) + 50;
|
||||
};
|
||||
Func<double,double> deriv = logx => {
|
||||
}
|
||||
double deriv(double logx)
|
||||
{
|
||||
double x = Math.Exp(logx);
|
||||
double vx = v*x + 1;
|
||||
return a - b*x + 0.5*(vx - m*m*x)/(vx*vx);
|
||||
};
|
||||
double vx = v * x + 1;
|
||||
return a - b * x + 0.5 * (vx - m * m * x) / (vx * vx);
|
||||
}
|
||||
// find where the likelihood matches the bound value
|
||||
List<double> zeroes = FindZeroes(func, deriv, stationaryPoints, inflectionPoints);
|
||||
logrmin = MMath.Min(zeroes);
|
||||
|
|
|
@ -678,24 +678,24 @@ namespace Microsoft.ML.Probabilistic.Factors
|
|||
//Console.WriteLine(StringUtil.CollectionToString(coeffs2, " "));
|
||||
List<double> inflectionPoints;
|
||||
GaussianOp_Slow.GetRealRoots(coeffs2, out inflectionPoints);
|
||||
Func<double, double> like = a => LogLikelihood(a, mProduct, vProduct, mA, pA, mB, vB);
|
||||
double like(double a) => LogLikelihood(a, mProduct, vProduct, mA, pA, mB, vB);
|
||||
var stationaryValues = stationaryPoints.ConvertAll(a => like(a));
|
||||
double max = MMath.Max(stationaryValues);
|
||||
double a0 = stationaryPoints[stationaryValues.IndexOf(max)];
|
||||
amode = a0;
|
||||
Func<double, double> func = a =>
|
||||
double func(double a)
|
||||
{
|
||||
return LogLikelihoodRatio(a, a0, mProduct, vProduct, mA, pA, mB, vB) + 50;
|
||||
};
|
||||
Func<double, double> deriv = a =>
|
||||
}
|
||||
double deriv(double a)
|
||||
{
|
||||
if (double.IsInfinity(a))
|
||||
return -a;
|
||||
double v = vProduct + vB * a * a;
|
||||
double diffv = (mProduct - a * mB)/v;
|
||||
double diffv = (mProduct - a * mB) / v;
|
||||
double diffa = a - mA;
|
||||
return a*vB*(diffv*diffv -1/v) + mB*diffv - diffa * pA;
|
||||
};
|
||||
return a * vB * (diffv * diffv - 1 / v) + mB * diffv - diffa * pA;
|
||||
}
|
||||
// find where the likelihood matches the bound value
|
||||
List<double> zeroes = GaussianOp_Slow.FindZeroes(func, deriv, stationaryPoints, inflectionPoints);
|
||||
amin = MMath.Min(zeroes);
|
||||
|
|
|
@ -134,7 +134,7 @@ namespace Microsoft.ML.Probabilistic.Factors
|
|||
|
||||
if ((afm1 < 0 || bf < 0) && mx * mz > 0)
|
||||
{
|
||||
double v_opt = 2 / 3 * (Math.Log(mx * mz / Ex2 / 2) - m);
|
||||
double v_opt = 2.0 / 3 * (Math.Log(mx * mz / Ex2 / 2) - m);
|
||||
if (v_opt != v)
|
||||
{
|
||||
bf = v * grad_S_v / (v_opt - v);
|
||||
|
|
|
@ -761,11 +761,7 @@ namespace Microsoft.ML.Probabilistic.Tests
|
|||
{
|
||||
// Make a temporary copy of the event to avoid a race condition
|
||||
// if the last subscriber unsubscribes immediately after the null check and before the event is raised.
|
||||
EventHandler<ProgressChangedEventArgs> handler = this.ProgressChanged;
|
||||
if (handler != null)
|
||||
{
|
||||
handler(this, e);
|
||||
}
|
||||
this.ProgressChanged?.Invoke(this, e);
|
||||
}
|
||||
|
||||
/// <summary>Reset all messages to their initial values. Sets NumberOfIterationsDone to 0.</summary>
|
||||
|
|
|
@ -1397,11 +1397,7 @@ namespace Microsoft.ML.Probabilistic.Tests
|
|||
{
|
||||
// Make a temporary copy of the event to avoid a race condition
|
||||
// if the last subscriber unsubscribes immediately after the null check and before the event is raised.
|
||||
EventHandler<ProgressChangedEventArgs> handler = this.ProgressChanged;
|
||||
if (handler != null)
|
||||
{
|
||||
handler(this, e);
|
||||
}
|
||||
this.ProgressChanged?.Invoke(this, e);
|
||||
}
|
||||
|
||||
/// <summary>Reset all messages to their initial values. Sets NumberOfIterationsDone to 0.</summary>
|
||||
|
|
|
@ -230,6 +230,17 @@ namespace Microsoft.ML.Probabilistic.Tests
|
|||
Gamma Precision, to_precision;
|
||||
Gaussian xActual, xExpected;
|
||||
|
||||
bool testImproper = false;
|
||||
if (testImproper)
|
||||
{
|
||||
// Test the case where precisionIsBetween = false
|
||||
X = Gaussian.FromNatural(1, 2);
|
||||
Mean = Gaussian.FromNatural(3, -1);
|
||||
Precision = Gamma.FromShapeAndRate(4, 5);
|
||||
to_precision = Gamma.FromShapeAndRate(6, 7);
|
||||
xActual = GaussianOp.SampleAverageConditional(X, Mean, Precision, to_precision);
|
||||
}
|
||||
|
||||
X = Gaussian.FromNatural(0.1559599323109816, 8.5162535450918462);
|
||||
Mean = Gaussian.PointMass(0.57957597647840942);
|
||||
Precision = Gamma.FromShapeAndRate(7.8308812008325587E+30, 8.2854255911709925E+30);
|
||||
|
|
|
@ -233,11 +233,11 @@ namespace Microsoft.ML.Probabilistic.Tests
|
|||
[Fact]
|
||||
public void VectorFromArrayOp_PointMassTest()
|
||||
{
|
||||
vectorFromArrayOp_PointMassTest(false);
|
||||
vectorFromArrayOp_PointMassTest(true);
|
||||
VectorFromArrayOp_HandlesPointMass(false);
|
||||
VectorFromArrayOp_HandlesPointMass(true);
|
||||
}
|
||||
|
||||
private void vectorFromArrayOp_PointMassTest(bool partial)
|
||||
private void VectorFromArrayOp_HandlesPointMass(bool partial)
|
||||
{
|
||||
int dim = 2;
|
||||
VectorGaussian to_vector = new VectorGaussian(dim);
|
||||
|
@ -712,16 +712,16 @@ namespace Microsoft.ML.Probabilistic.Tests
|
|||
{
|
||||
Gaussian a = new Gaussian(1, 2);
|
||||
Gaussian b = new Gaussian(4, 5);
|
||||
max_MaxPointMassTest(a, b);
|
||||
max_MaxPointMassTest(a, Gaussian.PointMass(2));
|
||||
max_MaxPointMassTest(a, Gaussian.PointMass(3));
|
||||
max_MaxPointMassTest(Gaussian.PointMass(2), b);
|
||||
max_MaxPointMassTest(Gaussian.PointMass(3), b);
|
||||
max_MaxPointMassTest(Gaussian.PointMass(3), Gaussian.PointMass(2));
|
||||
max_MaxPointMassTest(Gaussian.PointMass(2), Gaussian.PointMass(3));
|
||||
Max_MaxPointMass(a, b);
|
||||
Max_MaxPointMass(a, Gaussian.PointMass(2));
|
||||
Max_MaxPointMass(a, Gaussian.PointMass(3));
|
||||
Max_MaxPointMass(Gaussian.PointMass(2), b);
|
||||
Max_MaxPointMass(Gaussian.PointMass(3), b);
|
||||
Max_MaxPointMass(Gaussian.PointMass(3), Gaussian.PointMass(2));
|
||||
Max_MaxPointMass(Gaussian.PointMass(2), Gaussian.PointMass(3));
|
||||
}
|
||||
|
||||
private void max_MaxPointMassTest(Gaussian a, Gaussian b)
|
||||
private void Max_MaxPointMass(Gaussian a, Gaussian b)
|
||||
{
|
||||
double point = 3;
|
||||
Gaussian toPoint = MaxGaussianOp.MaxAverageConditional(Gaussian.PointMass(point), a, b);
|
||||
|
@ -745,18 +745,18 @@ namespace Microsoft.ML.Probabilistic.Tests
|
|||
//MaxGaussianOp.ForceProper = false;
|
||||
Gaussian max = new Gaussian(4, 5);
|
||||
Gaussian b = new Gaussian(1, 2);
|
||||
max_APointMassTest(new Gaussian(4, 1e-0), Gaussian.PointMass(0));
|
||||
max_APointMassTest(Gaussian.PointMass(11), Gaussian.PointMass(0));
|
||||
max_APointMassTest(max, b);
|
||||
max_APointMassTest(max, Gaussian.PointMass(2));
|
||||
max_APointMassTest(max, Gaussian.PointMass(3));
|
||||
max_APointMassTest(max, Gaussian.PointMass(4));
|
||||
max_APointMassTest(Gaussian.PointMass(3), b);
|
||||
max_APointMassTest(Gaussian.PointMass(4), b);
|
||||
max_APointMassTest(Gaussian.PointMass(3), Gaussian.PointMass(3));
|
||||
Max_APointMass(new Gaussian(4, 1e-0), Gaussian.PointMass(0));
|
||||
Max_APointMass(Gaussian.PointMass(11), Gaussian.PointMass(0));
|
||||
Max_APointMass(max, b);
|
||||
Max_APointMass(max, Gaussian.PointMass(2));
|
||||
Max_APointMass(max, Gaussian.PointMass(3));
|
||||
Max_APointMass(max, Gaussian.PointMass(4));
|
||||
Max_APointMass(Gaussian.PointMass(3), b);
|
||||
Max_APointMass(Gaussian.PointMass(4), b);
|
||||
Max_APointMass(Gaussian.PointMass(3), Gaussian.PointMass(3));
|
||||
}
|
||||
|
||||
private void max_APointMassTest(Gaussian max, Gaussian b)
|
||||
private void Max_APointMass(Gaussian max, Gaussian b)
|
||||
{
|
||||
double point = 3;
|
||||
Gaussian toPoint = MaxGaussianOp.AAverageConditional(max, Gaussian.PointMass(point), b);
|
||||
|
@ -3022,18 +3022,18 @@ weight * (tau + alphaX) + alphaX
|
|||
{
|
||||
Gaussian Product = Gaussian.FromMeanAndVariance(1.3, 0.1);
|
||||
Gaussian B = Gaussian.FromMeanAndVariance(1.24, 0.04);
|
||||
gaussianProductOp_APointMassTest(1, Product, B);
|
||||
GaussianProductOp_APointMass(1, Product, B);
|
||||
|
||||
Product = Gaussian.FromMeanAndVariance(10, 1);
|
||||
B = Gaussian.FromMeanAndVariance(5, 1);
|
||||
gaussianProductOp_APointMassTest(2, Product, B);
|
||||
GaussianProductOp_APointMass(2, Product, B);
|
||||
|
||||
Product = Gaussian.FromNatural(1, 0);
|
||||
gaussianProductOp_APointMassTest(2, Product, B);
|
||||
GaussianProductOp_APointMass(2, Product, B);
|
||||
}
|
||||
}
|
||||
|
||||
private void gaussianProductOp_APointMassTest(double aMean, Gaussian Product, Gaussian B)
|
||||
private void GaussianProductOp_APointMass(double aMean, Gaussian Product, Gaussian B)
|
||||
{
|
||||
bool isProper = Product.IsProper();
|
||||
Gaussian A = Gaussian.PointMass(aMean);
|
||||
|
|
|
@ -877,7 +877,7 @@ namespace Microsoft.ML.Probabilistic.Tests
|
|||
builder.Start.AddTransition('a', Weight.One).AddSelfTransition('b', Weight.One).AddTransition('c', Weight.One).SetEndWeight(Weight.One);
|
||||
var automaton = builder.GetAutomaton();
|
||||
Assert.Equal("ab*c", automaton.ToString(AutomatonFormats.Friendly));
|
||||
Assert.Equal("ab*c", automaton.ToString(AutomatonFormats.Regexp));
|
||||
Assert.Equal("ab*?c", automaton.ToString(AutomatonFormats.Regexp));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -661,7 +661,7 @@ namespace Microsoft.ML.Probabilistic.Tests
|
|||
var automaton = builder.GetAutomaton();
|
||||
|
||||
Assert.Equal("(a(c|bbb))*", automaton.ToString(AutomatonFormats.Friendly));
|
||||
Assert.Equal("(a(c|bbb))*", automaton.ToString(AutomatonFormats.Regexp));
|
||||
Assert.Equal("(a(c|bbb))*?", automaton.ToString(AutomatonFormats.Regexp));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -679,7 +679,7 @@ namespace Microsoft.ML.Probabilistic.Tests
|
|||
var automaton = builder.GetAutomaton();
|
||||
|
||||
Assert.Equal("(a|b)*", automaton.ToString(AutomatonFormats.Friendly));
|
||||
Assert.Equal("(a|b)*", automaton.ToString(AutomatonFormats.Regexp));
|
||||
Assert.Equal("(a|b)*?", automaton.ToString(AutomatonFormats.Regexp));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -716,7 +716,7 @@ namespace Microsoft.ML.Probabilistic.Tests
|
|||
builder.Start.AddTransition('!', Weight.One).SetEndWeight(Weight.One);
|
||||
var automaton = builder.GetAutomaton();
|
||||
Assert.Equal("(xyz)*!", automaton.ToString(AutomatonFormats.Friendly));
|
||||
Assert.Equal("(xyz)*!", automaton.ToString(AutomatonFormats.Regexp));
|
||||
Assert.Equal("(xyz)*?!", automaton.ToString(AutomatonFormats.Regexp));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -735,7 +735,7 @@ namespace Microsoft.ML.Probabilistic.Tests
|
|||
var automaton = builder.GetAutomaton();
|
||||
|
||||
Assert.Equal("xa*", automaton.ToString(AutomatonFormats.Friendly));
|
||||
Assert.Equal("xa*", automaton.ToString(AutomatonFormats.Regexp));
|
||||
Assert.Equal("xa*?", automaton.ToString(AutomatonFormats.Regexp));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
Загрузка…
Ссылка в новой задаче