Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Concurrent Programming - Investigating Task Messaging To Achieve Synchronization Free Inter-Task Communication

, 7 Jan 2008
Further studies of Parallel FX.
mandelbrot2.zip
Mandelbrot2
demo
ms
Ms
bin
Release
ms.vshost.exe
ms.vshost.exe.manifest
Clifton.Concurrent
Clifton.Tools.Data
Ms.csproj.user
Properties
Settings.settings
sci.math
doc
obj
Debug
ResolveAssemblyReference.cache
Release
ResolveAssemblyReference.cache
Properties
sci
doc
Properties
/* Copyright (c) 2006-2007 Coskun Oba
 * *****************************************************************************
 *  
 * SCI - Scientific Software Platform
 * Copyright (c) 2006-2007
 * All rights reserved.
 * 
 * Coskun Oba
 * oba.coskun@hotmail.com
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the <organization> nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY COSKUN OBA ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL COSKUN OBA BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * *****************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;

namespace Sci.Math
{
    #region Quaternion
    /// <summary>
    /// Represents a quaternion. Quaternions are non-commutative extension of complex numbers.
    /// A quaternion can be represented as q = q0 + q1i + q2j + q3k or q = (q0, q1, q2, q3)
    /// where q0, q1, q2 and q3 are real numbers and i*i=j*j=k*k=ijk=-1
    /// </summary>
    public struct Quaternion
    {
        private double[] components;

        /// <summary>
        /// Constructs a quaternion with a real component only.
        /// </summary>
        /// <param name="q0">Real component.</param>
        public Quaternion(double q0)
        {
            components = new double[] { q0, 0, 0, 0 };
        }

        /// <summary>
        /// Constructs a quaternion with real and ith components.
        /// </summary>
        /// <param name="q0">Real component.</param>
        /// <param name="q1">ith component.</param>
        public Quaternion(double q0, double q1)
        {
            components = new double[] { q0, q1, 0, 0 };
        }

        /// <summary>
        /// Constructs a quaternion with real, ith and jth components.
        /// </summary>
        /// <param name="q0">Real component.</param>
        /// <param name="q1">ith component.</param>
        /// <param name="q2">jth component.</param>
        public Quaternion(double q0, double q1, double q2)
        {
            components = new double[] { q0, q1, q2, 0 };
        }

        /// <summary>
        /// Constructs a quaternion from the given values.
        /// </summary>
        /// <param name="q0">Real component.</param>
        /// <param name="q1">ith component.</param>
        /// <param name="q2">jth component.</param>
        /// <param name="q3">kth component.</param>
        public Quaternion(double q0, double q1, double q2, double q3)
        {
            components = new double[] { q0, q1, q2, q3 };
        }

        /// <summary>
        /// Constructs a quaternion from the given complex scalar.
        /// </summary>
        /// <param name="z">Complex scalar to construct a quaternion from.</param>
        public Quaternion(ComplexNumber z)
        {
            components = new double[] { z.Re, z.Im, 0, 0 };
        }

        /// <summary>
        /// Copy constructor.
        /// </summary>
        /// <param name="q">Constructs a quaternion from the given quaternion.</param>
        public Quaternion(Quaternion q)
        {
            this.components = new double[4];
            q.components.CopyTo(this.components, 0);
        }

        #region Indexer
        /// <summary>
        /// Indexer
        /// </summary>
        /// <param name="index">Component index.</param>
        /// <returns>Gets/sets the component at the given index.</returns>
        public double this[int index]
        {
            get { return this.components[index]; }
            set { this.components[index] = value; }
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the modulus of this quaternion.
        /// </summary>
        public double Modulus
        {
            get { return System.Math.Sqrt(Sci.Math.Function.SumNthPow(components, 2)); }
        }

        /// <summary>
        /// Gets the conjugate of this quaternion.
        /// </summary>
        public Quaternion Conjugate
        {
            get { return new Quaternion(this[0], -this[1], -this[2], -this[3]); }
        }

        /// <summary>
        /// Gets the inverse of this quaternion.
        /// </summary>
        public Quaternion Inverse
        {
            get
            {
                if (this.Modulus == 0)
                    throw new MathException("Quaternion " + this.ToString() + " is NOT invertible!");

                return this.Conjugate / System.Math.Pow(Modulus, 2);
            }
        }
        #endregion

        #region Operator Overloading
        /// <summary>
        /// Represents the implicit coversion of a scalar to a Quaternion.
        /// </summary>
        /// <param name="scalar">A double precision scalar to be converted.</param>
        /// <returns>Returns a Quaternion.</returns>
        public static implicit operator Quaternion(double scalar)
        {
            return new Quaternion(scalar);
        }

        /// <summary>
        /// Represents the implicit coversion of a ComplexNumber to a Quaternion.
        /// </summary>
        /// <param name="z">A ComplexNumber to be converted.</param>
        /// <returns>Returns a Quaternion.</returns>
        public static implicit operator Quaternion(ComplexNumber z)
        {
            return new Quaternion(z);
        }

        /// <summary>
        /// Equality operator.
        /// Checks the equality of two given quaternions.
        /// </summary>
        /// <param name="rhs">Quaternion on the Right-Hand Side of the equality operator.</param>
        /// <param name="lhs">Quaternion on the Left-Hand Side of the equality operator.</param>
        /// <returns>Returns true if the given Quaternions are equal, false otherwise.</returns>
        public static bool operator ==(Quaternion rhs, Quaternion lhs)
        {
            return (rhs[0] == lhs[0] &&
                    rhs[1] == lhs[1] &&
                    rhs[2] == lhs[2] &&
                    rhs[3] == lhs[3]);
        }

        /// <summary>
        /// Inequality operator.
        /// Checks the inequality of two given quaternions.
        /// </summary>
        /// <param name="rhs">Quaternion on the Right-Hand Side of the equality operator.</param>
        /// <param name="lhs">Quaternion on the Left-Hand Side of the equality operator.</param>
        /// <returns>Returns true if the given Quaternions are not equal, false otherwise.</returns>
        public static bool operator !=(Quaternion rhs, Quaternion lhs)
        {
            return !(rhs == lhs);
        }

        /// <summary>
        /// Unary negation operator.
        /// Negates the given Quaternion.
        /// </summary>
        /// <param name="q">Quaternion to be negated.</param>
        /// <returns>
        /// Returns a Quaternion which is the negative equavalent of the given Quaternion.
        /// </returns>
        public static Quaternion operator -(Quaternion q)
        {
            return new Quaternion(-q[0], -q[1], -q[2], -q[3]);
        }

        /// <summary>
        /// Binary plus operator.
        /// </summary>
        /// <param name="op1">First operand.</param>
        /// <param name="op2">Second operand.</param>
        /// <returns>Returns a Quaternion which represents the q = op1 + op2 operation.</returns>
        public static Quaternion operator +(Quaternion op1, Quaternion op2)
        {
            return new Quaternion(  op1[0] + op2[0], 
                                    op1[1] + op2[1], 
                                    op1[2] + op2[2], 
                                    op1[3] + op2[3]);
        }

        /// <summary>
        /// Binary minus operator.
        /// </summary>
        /// <param name="op1">First operand.</param>
        /// <param name="op2">Second operand.</param>
        /// <returns>Returns a Quaternion which represents the q = op1 - op2 operation.</returns>
        public static Quaternion operator -(Quaternion op1, Quaternion op2)
        {
            return op1 + (-op2);
        }


        /// <summary>
        /// Binary multiplication operator.
        /// </summary>
        /// <param name="scalar">Scalar.</param>
        /// <param name="q">Quaternion.</param>
        /// <returns>Returns a Quaternion.</returns>
        public static Quaternion operator *(double scalar, Quaternion q)
        {
            return new Quaternion(  q[0] * scalar,
                                    q[1] * scalar,
                                    q[2] * scalar,
                                    q[3] * scalar);
        }

        /// <summary>
        /// Binary multiplication operator.
        /// </summary>
        /// <param name="q">Quaternion.</param>
        /// <param name="scalar">Scalar.</param>
        /// <returns>Returns a Quaternion.</returns>
        public static Quaternion operator *(Quaternion q, double scalar)
        {
            return scalar * q;
        }

        /// <summary>
        /// Binary multiplication operator.
        /// </summary>
        /// <param name="op1">First operand.</param>
        /// <param name="op2">Second operand.</param>
        /// <returns>Returns a Quaternion which represents the q = op1 * op2 operation.</returns>
        public static Quaternion operator *(Quaternion op1, Quaternion op2)
        {
            Quaternion q = new Quaternion(0);
            q[0] = op1[0] * op2[0] - op1[1] * op2[1] - op1[2] * op2[2] - op1[3] * op2[3];
            q[1] = op1[0] * op2[1] + op1[1] * op2[0] + op1[2] * op2[3] - op1[3] * op2[2];
            q[2] = op1[0] * op2[2] + op1[2] * op2[0] + op1[3] * op2[0] - op1[0] * op2[3];
            q[3] = op1[0] * op2[3] + op1[3] * op2[0] + op1[0] * op2[1] - op1[1] * op2[0];

            return q;
        }

        /// <summary>
        /// Binary multiplication operator.
        /// </summary>
        /// <param name="q">Qauternion.</param>
        /// <param name="scalar">Scalar.</param>
        /// <returns>Returns a Quaternion.</returns>
        public static Quaternion operator /(Quaternion q, double scalar)
        {
            if (scalar == 0)
                throw new DivideByZeroException();

            return new Quaternion(  q[0] / scalar, 
                                    q[1] / scalar, 
                                    q[2] / scalar, 
                                    q[3] / scalar);
        }

        /// <summary>
        /// Binary multiplication operator.
        /// </summary>
        /// <param name="scalar">Scalar.</param>
        /// <param name="q">Quaternion.</param>
        /// <returns>Returns a Quaternion.</returns>
        public static Quaternion operator /(double scalar, Quaternion q)
        {
            return scalar * q.Inverse;
        }

        /// <summary>
        /// Binary division operator.
        /// </summary>
        /// <param name="op1">First operand.</param>
        /// <param name="op2">Second operand.</param>
        /// <returns>Returns a Quaternion which represents the q = op1 / op2 operation.</returns>
        public static Quaternion operator /(Quaternion op1, Quaternion op2)
        {
            return op1 * op2.Inverse;
        }
        #endregion

        /// <summary>
        /// Represents the inner product of two given quaternions.
        /// </summary>
        /// <param name="op1">First operand.</param>
        /// <param name="op2">Second operand.</param>
        /// <returns>Returns the value of the inner product.</returns>
        public static double InnerProduct(Quaternion op1, Quaternion op2)
        {
            return  op1[0] * op2[0] + 
                    op1[1] * op2[1] + 
                    op1[2] * op2[2] + 
                    op1[3] * op2[3];
        }

        #region Overriden Methods
        /// <summary>
        /// Checks whether the given object is equal to this Quaternion.
        /// </summary>
        /// <param name="obj">Object to be checked if it is equal to this Quaternion.</param>
        /// <returns>Returns True if the given object is equal to this Quaternion, false otherwise.</returns>
        public override bool Equals(object obj)
        {
            return ((obj is Quaternion)) ? this == (Quaternion)obj : false;
        }

        /// <summary>
        /// Hashcode that reprsents this Quaternion.
        /// </summary>
        /// <returns>Returns integer value of the hashcode.</returns>
        public override int GetHashCode()
        {
            return  this[0].GetHashCode() ^ 
                    this[1].GetHashCode() ^ 
                    this[2].GetHashCode() ^ 
                    this[3].GetHashCode();
        }

        /// <summary>
        /// String representation of this Quaternion.
        /// </summary>
        /// <returns>Returns the string representation of this Quaternion.</returns>
        public override string ToString()
        {
            return  "(" + this[0].ToString() + " + " + 
                    this[1].ToString() + "i + " + 
                    this[2].ToString() + "j + " + 
                    this[3].ToString() + "k" + ")";
        }
        #endregion
    }
    #endregion
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Marc Clifton

United States United States
Marc is the creator of two open source projets, MyXaml, a declarative (XML) instantiation engine and the Advanced Unit Testing framework, and Interacx, a commercial n-tier RAD application suite.  Visit his website, www.marcclifton.com, where you will find many of his articles and his blog.
 
Marc lives in Philmont, NY.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 7 Jan 2008
Article Copyright 2008 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid