Matrix class : Matrix « Data Types « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Data Types » MatrixScreenshots 
Matrix class
 
//http://calcsharp.codeplex.com/license
//Microsoft Public License (Ms-PL)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Drawing;

namespace CalcSharp.Core.Containers
{
    [Serializable]
    public abstract class AMatrix : IFormattable, ICloneable, IEquatable<AMatrix>
    {
        private int nRows, nCols;

        protected AMatrix(int rows, int columns)
        {
            if (rows < 1throw new ArgumentException("must be greater than 0""rows");
            if (columns < 1throw new ArgumentException("must be greater than 0""columns");
            nRows = rows;
            nCols = columns;
        }

        #region Properties
        public virtual double this[int row, int column]
        {
            get
            {
                RangeCheck(row, column);
                return ValueAt(row, column);
            }
            set
            {
                RangeCheck(row, column);
                ValueAt(row, column, value);
            }
        }

        public int Columns
        {
            get return nCols; }
            protected set nCols = value; }
        }

        public int Rows
        {
            get return nRows; }
            protected set nRows = value; }
        }
        #endregion

        #region Interface Implementations
        public override bool Equals(object obj)
        {
            return Equals(obj as AMatrix);
        }

        public bool Equals(AMatrix other)
        {
            // Reject equality when the argument is null or has a different shape.
            if (other == null)
            {
                return false;
            }
            if (Columns != other.Columns || Rows != other.Rows)
            {
                return false;
            }

            // Accept if the argument is the same object as this.
            if (ReferenceEquals(this, other))
            {
                return true;
            }

            // If all else fails, perform elementwise comparison.
            for (int i = 0; i < Rows; i++)
            {
                for (int j = 0; j < Columns; j++)
                {
                    if (ValueAt(i, j!= other.ValueAt(i, j))
                    {
                        return false;
                    }
                }
            }

            return true;
        }

        public override int GetHashCode()
        {
            int hashNum = System.Math.Max(Rows * Columns, 25);
            double[] hashBase = new double[hashNum];
            for (int i = 0; i < hashNum; i++)
            {
                int col = i % Columns;
                int row = (i - col/ Rows;
                hashBase[ithis[row, col];
            }
            return hashBase.GetHashCode();
        }

        object ICloneable.Clone()
        {
            return Clone();
        }

        public virtual AMatrix Clone()
        {
            AMatrix result = CreateMatrix(Rows, Columns);
            CopyTo(result);
            return result;
        }

        public virtual string ToString(string format, IFormatProvider formatProvider)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < Rows; i++)
            {
                for (int j = 0; j < Columns; j++)
                {
                    sb.Append(ValueAt(i, j).ToString(format, formatProvider));
                    if (j != Columns - 1)
                    {
                        sb.Append(", ");
                    }
                }
                if (i != Rows - 1)
                {
                    sb.Append(Environment.NewLine);
                }
            }
            return sb.ToString();
        }
        #endregion

        private void RangeCheck(int row, int column)
        {
            if (row < || row >= nRowsthrow new ArgumentOutOfRangeException("row");
            if (column < || column >= nColsthrow new ArgumentOutOfRangeException("column");
        }

        public virtual void CopyTo(AMatrix target)
        {
            if (target == nullthrow new ArgumentNullException("target");
            if (Rows != target.Rows || Columns != target.Columnsthrow new Exception("Target & Source rows/column count mismatch");

            for (int i = 0; i < this.nRows; i++)
            {
                for (int j = 0; j < this.nCols; j++)
                {
                    target.ValueAt(i, j, this.ValueAt(i, j));
                }
            }
        }

        public void Negate()
        {
            for (int i = 0; i < Rows; i++)
            {
                for (int j = 0; j < Columns; j++)
                {
                   this.ValueAt(i, j, -1*this.ValueAt(i, j));
                }
            }
        }

        #region Abstract parts
        protected internal abstract void ValueAt(int row, int column, double value);
        protected internal abstract double ValueAt(int row, int column);
        protected internal abstract AMatrix CreateMatrix(int numberOfRows, int numberOfColumns);
        public abstract double Determinant();
        public abstract AMatrix Transpose();
        public abstract AMatrix Inverse();
        public abstract void Clear();
        public abstract double[] GetRow(int index);
        public abstract double[] GetColumn(int index);
        public abstract void SetColumn(int index, double[] source);
        public abstract void SetRow(int index, double[] source);
        public abstract Bitmap GetPreview();
        #endregion
    }
}

   
  
Related examples in the same category
1.Inverts a Matrix
2.Matrix used in linear algebra
3.Matrix 3 x 3
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.