Scalar Class 
Namespace: AGI.Foundation.Geometry
The Scalar type exposes the following members.
Name  Description  

Scalar  Initializes a new instance.  
Scalar(Scalar, CopyContext)  Initializes a new instance as a copy of an existing instance. 
Name  Description  

IsFrozen 
Gets a value indicating whether this object is frozen. A frozen object cannot be modified and an
ObjectFrozenException will be thrown if an attempt is made to do so.
(Inherited from DefinitionalObject.) 
Name  Description  

Add(Double) 
Adds a fixed scalar to this one.
 
Add(Scalar) 
Adds another scalar to this one.
 
CheckForSameDefinition(DefinitionalObject) 
Checks to determine if another instance has the same definition as this instance and
returns if it does. Derived classes MUST override this method and check
all new fields introduced by the derived class for definitional equivalence. It is NOT necessary
to check base class fields because the base class will already have done that. When overriding this method,
you should NOT call the base implementation because it will return for all derivedclass instances.
Derived classes should check the type of other to preserve the symmetric nature of IsSameDefinition(Object).
(Overrides DefinitionalObjectCheckForSameDefinition(DefinitionalObject).)  
CheckForSameDefinition(Scalar) 
Checks to determine if another instance has the same definition as this instance and
returns if it does. Derived classes MUST override this method and check
all new fields introduced by the derived class for definitional equivalence. It is NOT necessary
to check base class fields because the base class will already have done that. When overriding this method,
you should NOT call the base implementation because it will return for all derivedclass instances.
Derived classes should check the type of other to preserve the symmetric nature of IsSameDefinition(Object).
 
Clone 
Clones this object using the specified context.
(Inherited from DefinitionalObject.)  
ComputeCurrentDefinitionHashCode 
Computes a hash code based on the current properties of this object. Derived classes MUST override this
method and compute a hash code that combines: a unique hash code seed, the base implementation result, and
the hash codes of all new fields introduced by the derived class which are used in the
CheckForSameDefinition(DefinitionalObject) method.
(Overrides DefinitionalObjectComputeCurrentDefinitionHashCode.)  
CreateScalarDerivative 
Constructs a scalar which represents a derivative of this scalar.
 
Divide(Double) 
Divides this scalar (numerator) by a fixed value (denominator).
 
Divide(Scalar) 
Divides this scalar (numerator) by another one (denominator).
 
EnumerateDependencies 
Enumerates the dependencies of this object by calling
EnumerateT(T) for each object that this object directly depends upon.
Derived classes which contain additional dependencies MUST override this method, call the base
implementation, and enumerate dependencies introduced by the derived class.
(Inherited from DefinitionalObject.)  
Equals  Determines whether the specified object is equal to the current object. (Inherited from Object.)  
Finalize  Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)  
Freeze 
Freezes this object. Further attempts to modify it will result
in an ObjectFrozenException.
(Inherited from DefinitionalObject.)  
FreezeAggregatedObjects 
Called by Freeze to also freeze any objects that are considered to be a part of this object.
Derived classes which contain additional aggregated objects MUST override this method, call the base
implementation, and freeze aggregated objects introduced by the derived class. The objects that need to be
frozen in this method are frequently created in this object's constructor and are not settable via
properties.
(Inherited from DefinitionalObject.)  
GetDefinitionHashCode 
Gets a hash code representing the definition of this object.
(Inherited from DefinitionalObject.)  
GetEvaluator 
Gets an evaluator that can be used to find the value of this scalar function at a given JulianDate.
 
GetEvaluator(EvaluatorGroup) 
Gets an evaluator that can be used to find the value of this scalar function at a given JulianDate.
Adds the evaluator to the EvaluatorGroup.
 
GetHashCode  Serves as the default hash function. (Inherited from Object.)  
GetType  Gets the Type of the current instance. (Inherited from Object.)  
IsSameDefinition 
Determines if this object has the same definition as another object.
(Inherited from DefinitionalObject.)  
MemberwiseClone  Creates a shallow copy of the current Object. (Inherited from Object.)  
Multiply(Double) 
Multiplies this scalar with a fixed value.
 
Multiply(Scalar) 
Multiplies another scalar with this one.
 
Power 
Raises this scalar to the given fixed exponent.
 
Subtract(Double) 
Subtracts a fixed scalar from this one.
 
Subtract(Scalar) 
Subtracts another scalar from this one.
 
ThrowIfFrozen 
Throws ObjectFrozenException if this object IsFrozen.
This method should be called from any method or property that modifies this object.
(Inherited from DefinitionalObject.)  
ToString  Returns a string that represents the current object. (Inherited from Object.) 
Name  Description  

Addition(Double, Scalar) 
Adds two scalars together.
 
Addition(Scalar, Scalar) 
Adds two scalars together.
 
Addition(Scalar, Double) 
Adds two scalars together.
 
Division(Double, Scalar) 
Divides one scalar from another (left/right).
 
Division(Scalar, Scalar) 
Divides one scalar from another (left/right).
 
Division(Scalar, Double) 
Divides one scalar from another (left/right).
 
(Double to Scalar) 
Converts a constant double value to a Scalar.
 
Multiply(Double, Scalar) 
Multiplies two scalars together.
 
Multiply(Scalar, Scalar) 
Multiplies two scalars together.
 
Multiply(Scalar, Double) 
Multiplies two scalars together.
 
Subtraction(Double, Scalar) 
Subtracts one scalar from another.
 
Subtraction(Scalar, Scalar) 
Subtracts one scalar from another.
 
Subtraction(Scalar, Double) 
Subtracts one scalar from another.

This example shows how to create a new Scalar subclass:
// Create a new type derived from Scalar. This type will represent the square root of // another scalar. public class SquareRootScalar : Scalar { public SquareRootScalar() { } public SquareRootScalar(Scalar scalarToTakeSquareRootOf) { m_scalar = scalarToTakeSquareRootOf; } // The copy constructor, used to make a copy of the object. Copy all of the // fields of the 'existingInstance' to the new object. Reference types should // be passed through a call to UpdateReference so that the depth of the copy // can be controlled by the user. See the documentation of the ICloneWithContext // interface for more information. protected SquareRootScalar(SquareRootScalar existingInstance, CopyContext context) : base(existingInstance, context) { m_scalar = context.UpdateReference(existingInstance.m_scalar); } // This is called to make a copy of the object, which it does by calling the // copy constructor above. public override object Clone(CopyContext context) { return new SquareRootScalar(this, context); } // This method is only called by the IsSameDefinition method in the base class to // determine if two scalars are equivalent. Derived classes MUST override this method and // check all new fields introduced by the derived class for definitional equivalence. It // is NOT necessary to check base class fields because the base class will already have // done that. protected sealed override bool CheckForSameDefinition(Scalar other) { SquareRootScalar o = other as SquareRootScalar; return o != null && AreSameDefinition(m_scalar, o.m_scalar); } // Called to determine a hash code for the current configuration of this object. // Derived classes MUST override this method and compute a hash code that combines: // a unique hash code seed, the base implementation result, and the // hash codes of all new fields introduced by the derived class which are used // in the CheckForSameDefinition method. protected override int ComputeCurrentDefinitionHashCode() { return HashCode.Combine(140049223, // random number specific to this object base.ComputeCurrentDefinitionHashCode(), GetDefinitionHashCode(m_scalar)); } // Called to enumerate all of the other objects on which this object depends. This // allows clients to navigate the graph of objects related to a computation. public override void EnumerateDependencies(DependencyEnumerator enumerator) { base.EnumerateDependencies(enumerator); enumerator.Enumerate(m_scalar); } // The scalar that this scalar is the square root of. public Scalar ScalarToTakeSquareRootOf { get { return m_scalar; } set { m_scalar = value; } } // This method is responsible for returning an instance of the private // Evaluator class. It should ensure that the properties are not null or // in an invalid state, and then use the evaluator group when it constructs // and returns an instance of the Evaluator. public override ScalarEvaluator GetEvaluator(EvaluatorGroup group) { if (group == null) throw new ArgumentNullException("group"); // Ensure that the properties are not null. if (ScalarToTakeSquareRootOf == null) throw new PropertyInvalidException("ScalarToTakeSquareRootOf", PropertyInvalidException.PropertyCannotBeNull); return group.CreateEvaluator<ScalarEvaluator>(CreateEvaluator); } // This method, which is passed to the evaluator group in the method above as a delegate, // will only be called by the delegate if the evaluator does not yet exist in the group // and needs to be created. private ScalarEvaluator CreateEvaluator(EvaluatorGroup group) { // In order to take the square root of the scalar, we must first evaluate it. // Get the evaluator that will allow us to do so. // Notice that we create this evaluator in the same EvaluatorGroup. var scalarEvaluator = ScalarToTakeSquareRootOf.GetEvaluator(group); return new Evaluator(group, scalarEvaluator); } private Scalar m_scalar; // This is the definition of the Evaluator that is used to actually evaluate this Scalar. // Because it is a private, nested class, it is not visible outside of the SquareRootScalar class. // This is ok, though, because once it is created users only interact with it via a reference // to its base class: ScalarEvaluator. private sealed class Evaluator : ScalarEvaluator { // An evaluator should not store any data that the user will be able to change // after creating the evaluator. This sometimes requires that data required by the // evaluator be copied or frozen using the IFreezable interface. public Evaluator(EvaluatorGroup group, ScalarEvaluator scalarEvaluator) : base(group) { m_scalarEvaluator = scalarEvaluator; } // The Evaluator's copy constructor will be called from the Clone method. // Don't forget to call the base class implementation! private Evaluator(Evaluator existingInstance, CopyContext context) : base(existingInstance, context) { // For nonevaluator reference types, we would use context.UpdateReference to // allow the context to update the references we hold. // This evaluator does not have any nonevaluator reference fields. // For evaluators, just assign the reference directly  we'll call UpdateReference later. m_scalarEvaluator = existingInstance.m_scalarEvaluator; // Always call UpdateEvaluatorReferences at the end of the copy constructor. // This is where references to evaluators will be updated. UpdateEvaluatorReferences(context); } // This method is used by the EvaluatorGroup system to avoid redundant evaluations. The // EvaluatorGroup may call it on your evaluator in order to replace your evaluator's // reference to another evaluator with a reference to a version that caches its last // result. public override void UpdateEvaluatorReferences(CopyContext context) { m_scalarEvaluator = context.UpdateReference(m_scalarEvaluator); } // The Clone method should call the copy constructor. public override object Clone(CopyContext context) { return new Evaluator(this, context); } // This method determines if there is data available from this Evaluator at // the specified date. public override bool IsAvailable(JulianDate date) { // This evaluator is available whenever the nested evaluator is available. return m_scalarEvaluator.IsAvailable(date); } // This method returns a collection of time intervals when data is // available from this Evaluator. public override TimeIntervalCollection GetAvailabilityIntervals(TimeIntervalCollection consideredIntervals) { // This evaluator is available whenever the nested evaluator is available. return m_scalarEvaluator.GetAvailabilityIntervals(consideredIntervals); } // This property determines if this Evaluator can safely be used from multiple threads // simultaneously. If the evaluator stores data during the Evaluate call, it is not thread // safe. Otherwise, it generally is thread safe as long as any nested evaluators it uses // are thread safe. public override bool IsThreadSafe { get { // This evaluator is thread safe as long as the nested evaluator is thread safe. return m_scalarEvaluator.IsThreadSafe; } } // This property determines if this Evaluator result changes depending on the time at which it is evaluated. public override bool IsTimeVarying { get { return m_scalarEvaluator.IsTimeVarying; } } // This is where we do the actual evaluation when only the value of the scalar (not additional derivatives) // is required. public override double Evaluate(JulianDate date) { double value = m_scalarEvaluator.Evaluate(date); return Math.Sqrt(value); } // This is where we do the actual evaluation when additional derivatives of the scalar // are requested as well. public override Motion<double> Evaluate(JulianDate date, int order) { Motion<double> motion = m_scalarEvaluator.Evaluate(date, order); // Since only the second derivative is implemented here, constrain the array to // only return up to that order. int size = Math.Min(motion.Order, 2); double[] result = new double[size + 1]; result[0] = Math.Sqrt(motion[0]); // If the user did not request higher derivatives, do not spend time calculating them. if (order > 0) { // Compute the derivative using the chain rule. result[1] = 0.5 * Math.Pow(motion[0], 0.5) * motion[1]; } if (order > 1) { // Compute the derivative using the product rule and the chain rule. result[2] = 0.5 * Math.Pow(motion[0], 0.5) * motion[2]  0.25 * Math.Pow(motion[0], 1.5) * motion[1] * motion[1]; } return new Motion<double>(result); } // Override the Dispose method to call the Dispose() method on any nested // evaluators or other disposable nested types. protected override void Dispose(bool disposing) { if (disposing) { m_scalarEvaluator.Dispose(); } } private ScalarEvaluator m_scalarEvaluator; } }