Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

matric multiplication

using System;

class MainClass {
  public static void Main (string[] args) {
    var marixOne = new Matrix(2,3,new int[6]{3,2,1,2,0,-1});
    var marixTwo = new Matrix(3,2,new int[6]{3,1,-2,0,1,2});
    var idea = marixOne * marixTwo;
    marixOne.Print();
    marixTwo.Print();
    idea.Print();
  }
}

// Using one dimensional array .
public class Matrix{

  public int row;
  public int column;
  public int[] arrayContainer;

  public Matrix(int numberOfrow , int numberOfcol, int[] containerArray){
    row = numberOfrow;
    column = numberOfcol;
    arrayContainer = new int [row * column];
    for(int i = 0; i < row*column ;i++){arrayContainer[i]= containerArray[i];}
  }



  public static Matrix operator *(Matrix a , Matrix b){

    var ispossible = a.column == b.row;
    if(!ispossible) return null;
    var numberofItemsInArray = a.row * b.column ;
    var resultArray = new int[numberofItemsInArray];
    var count = 0;
    var resultitem = 0;

      for(int currentRow =0 ; currentRow < a.row ; currentRow++)
      {
        for(int currentColoumn = 0 ; currentColoumn < b.column ; currentColoumn++ )
        {
          for(int index = 0 ; index < b.row;index++){

            var row_item = a.arrayContainer[index + a.column * currentRow];
            var coloumn_item = b.arrayContainer[currentColoumn + b.column * index ];
            resultitem += row_item * coloumn_item ;
            /*
            Console.WriteLine($"row_item = {row_item},coloumn_item = {coloumn_item},multiplication = {row_item * coloumn_item } resultitem = {resultitem }");
            */
          }
         
          /*Console.WriteLine("--
--"); */
          resultArray[count] = resultitem ;
          resultitem = 0;
          count++;
         
        }
      }
      return new Matrix(a.row,b.column,resultArray);
    }


  public void Print(){
    Console.WriteLine("
");
    for(int currentRow = 0 ; currentRow<row ; currentRow++ ){
      var item = "";
      for(int currentcoloumn = 0  ; currentcoloumn<column ; currentcoloumn++)
      {
        item +=" "+arrayContainer[currentcoloumn+column*currentRow];
      }
      Console.WriteLine(item);
      item = string.Empty;
    }
    Console.WriteLine("

");
  }

}
Comment

PREVIOUS NEXT
Code Example
Csharp :: unity roam, chase, attack states 
Csharp :: wpf settings core 
Csharp :: godot c# signal 
Csharp :: C# Blocks without statements 
Csharp :: transformquestionmarks=OCR 
Csharp :: In ASP.NET Core how check if request is local 
Csharp :: c# boolean 
Csharp :: c# check file similarities 
Csharp :: c# place all keys in dictionary into array 
Csharp :: how to backup terrain in unity 
Csharp :: ddsfsd 
Csharp :: grab reference from method parameter c# 
Csharp :: .net core best package for scheduler 
Csharp :: .net core string compare ignore case and accents 
Csharp :: CullingGroup 
Csharp :: publish applications for linux 
Csharp :: ExpandoObject Make Objects Extensible 
Csharp :: c# textbox tab column 
Csharp :: Delegate with parameter and return 
Csharp :: Compiling C# Example 
Csharp :: how to define a static color resource in xaml wpf 
Csharp :: copy properties from two subclasses c# 
Csharp :: how to set window position 
Csharp :: return array in c# 
Csharp :: unity how to check object position 
Csharp :: loop c# 
Csharp :: c# second last element 
Csharp :: c# code process to start any exe application 
Csharp :: read administrator account remote machine C# 
Html :: fevicon 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =