Vector and Matrix typesInfer.NET includes types for representing vectors (1D arrays of double values) and matrices (2D rectangular arrays of double values). The Vector typeThe Vector class is a comprehensive class for doing efficient operations on vectors of doubles. The Vector class is actually an abstract base class that has four concrete implementations: namely DenseVector, SparseVector, ApproximateSparseVector and PiecewiseVector. However, most of the time you should interact with the Vector class which provides a common gateway for interacting with all the different vector types. The non-dense vector types are useful when you have vectors where most of the elements have the same value or approximately the same value. This is common, for example, when working with large Dirichlet and Discrete distributions, or distributions over characters. The sparsity is specified using the Sparsity class described in the next section. SparsitySparsity specifications, which are used in the Vector factory methods described in a later section, can be created using the Sparsity class. This class contains several static properties or methods for creating different sparsity specifications as shown in the following table. Once created, the properties of a sparsity specification cannot be changed.
Factory MethodsTo create a vector, use one of the following static methods on the Vector class. These include settings for the desired sparsity of the generated vector:
There are many categories of functions on Vector instances which allow efficient processing. These are summarised below; for full details, see the code documentation for the Vector class. IndexingIndexing allows the getting and setting of individual elements of the vector. This is only recommended in exceptional circumstances as it is typically an inefficient way to process vectors. Other methods relating to indexing are IndexOfAll, IndexOf, IndexOfMin, and IndexOfMax. AppendingVectors can be created by appending one vector or scalar to another vector by means of the Append method. A static version of this, Concat, is also available. SetToAn existing Vector instance can be modified by setting its values to a set of given values. Instance methods SetTo, SetToSubarray, and SetAllElements provide this functionality. The CopyTo method provides the reverse functionality, allowing the current instance to be copied to another specified Vector instance. SetToFunctionThe Vector class has some methods to set the current instance to a combination of one or two other vectors. One general such method is SetToFunction which takes one or two vectors along with a delegate giving the element-wise calculation. Specific such methods include SetToSum, SetToDifference, SetToProduct, SetToRatio and SetToPower. OperatorsOperators +, - (unary and binary), *, / and ^ are all overridden to provide addition, subtraction/negation, product, division, and raising to a power. >, >=, <, <=, =, != are all overridden to provide vector comparison. These comparison operators make use of two general methods All and Any which take a condition delegate. ReduceThis category of methods allows one or more vectors to be reduced to a scalar by means of a combining function. The most general method is Reduce which takes a general combining delegate as an argument. Specific versions include Sum, SumI, SumISq, Max, Min, and Inner. Operations involving matricesMethods such as SetToProduct (with a Matrix and Vector argument), PredivideBy, Outer, and SetToDiagonal are provided to support some fundamental operations involving vectors and matrices. Although these will work with any type of vector (sparse or dense), they will be quite inefficient for sparse vectors, and there is no support for sparse matrices in Infer.NET. The PositiveDefiniteMatrix typeThe PostiveDefiniteMatrix class is used to represent a positive definite matrix which is the domain type for a Wishart distribution. Wishart distributions are used to represent uncertainty in the precision matrix of a VectorGaussian distribution, and many of the methods in the PostiveDefiniteMatrix class are geared towards this usage. For full details see the code documentation for the PositiveDefiniteMatrix class. |