Пример #1
0
    /**
     * Allows SMatrices "multiplication" as an operation
     * Throws an exception if formats do not match
     */
    public static SMatrix operator *(SMatrix a, SMatrix b)
    {
        if (a.cols == b.rows)
        {
            // Pass through every row and every column
            SMatrix result = new SMatrix(a.rows, b.cols);

            for (int row = 0; row < a.rows; row++)
            {
                for (int col = 0; col < b.cols; col++)
                {
                    // For each element in row a multiply with col b
                    float total = 0;
                    for (int row_b = 0; row_b < b.rows; row_b++)
                    {
                        total += a.data[row, row_b] * b.data[row_b, col];
                    }
                    result.set(row, col, total);
                }
            }

            return(result);
        }
        else
        {
            throw new InvalidOperationException("Cannot perform multiplication on SMatrices of different sizes.");
        }
    }
Пример #2
0
    /**
     * Joins two matrices of the horizontal axis. Throws an exception if the formats are not compatible.
     */
    public static SMatrix join_horizontal(SMatrix a, SMatrix b)
    {
        if (a.rows == b.rows)
        {
            float[,] result = new float[a.rows, a.cols + b.cols];

            for (int r = 0; r < a.rows; r++)
            {
                for (int c = 0; c < a.cols; c++)
                {
                    result[r, c] = a.data[r, c];
                }
            }

            for (int r = 0; r < b.rows; r++)
            {
                for (int c = 0; c < b.cols; c++)
                {
                    result[r, c + a.cols] = b.data[r, c];
                }
            }

            return(new SMatrix(result));
        }
        else
        {
            throw new InvalidOperationException("Cannot join SMatrices horizontally of different row amounts.");
        }
    }
Пример #3
0
    /**
     * Allows SMatrix "multiplication" with a salar as an operation, but the scalar is first
     */
    public static SMatrix operator *(float b, SMatrix a)
    {
        SMatrix result = new SMatrix(a.rows, a.cols);

        for (int row = 0; row < a.rows; row++)
        {
            for (int col = 0; col < a.cols; col++)
            {
                result.set(row, col, (a.data[row, col] * b));
            }
        }

        return(result);
    }
Пример #4
0
 /**
  * Static version of the transpose function. This time, it returns a full NEW SMatrix as a result.
  */
 public static SMatrix transpose(SMatrix a)
 {
     // Do not call the object's version because it changes the objects value
     // Reversed rows and cols because of transposition
     float[,] result = new float[a.cols, a.rows];
     for (int y = 0; y < a.rows; y++)
     {
         for (int x = 0; x < a.cols; x++)
         {
             result[x, y] = a.data[y, x];
         }
     }
     return(new SMatrix(result));
 }
Пример #5
0
    /**
     * Used by the det function to recursively get the sub determinants of the matrix
     */
    private float sub_det(SMatrix sub)
    {
        float det = 0;

        if (sub.rows == sub.cols)
        {
            int size = sub.rows;
            if (size > 2)
            {
                // We need to find the det again
                int mult = 1;
                for (int i = 0; i < size; i++)
                {
                    float   dominant_value = sub.data[0, i];
                    SMatrix low_sub        = new SMatrix(new float[(size - 1), (size - 1)]);

                    // Construct dependant sub low level SMatrix
                    int added_amount = 0;
                    for (int y = 0; y < size; y++)
                    {
                        for (int x = 0; x < size; x++)
                        {
                            if (y != 0 && x != i)
                            {
                                // We can construct with these
                                int y_index = (int)Mathf.Floor((float)added_amount / (float)(size - 1));
                                int x_index = added_amount % (size - 1);
                                low_sub.data[y_index, x_index] = sub.data[y, x];
                                added_amount++;
                            }
                        }
                    }

                    det  += mult * dominant_value * sub_det(low_sub);
                    mult *= -1;
                }
                return(det);
            }
            else
            {
                // We are in a 2x2
                return((sub.data[0, 0] * sub.data[1, 1]) - (sub.data[0, 1] * sub.data[1, 0]));
            }
        }
        else
        {
            throw new InvalidOperationException("Cannot get the determinant of a m * n SMatrix.");
        }
    }
Пример #6
0
        public void TestMatrix()
        {
            SMatrix matrix1 = new SMatrix();
            SMatrix matrix2 = new SMatrix();

            int[,] arr1 = new int[8, 10];
            int[,] arr2 = new int[3, 3];

            Console.WriteLine("Matrix 1: ");
            for (int y = 0; y < arr1.GetLength(1); y++)
            {
                for (int x = 0; x < arr1.GetLength(0); x++)
                {
                    arr1[x, y] = 3;
                    Console.Write(arr1[x, y] + "| ");
                }
                Console.WriteLine();
            }

            Console.WriteLine("Matrix 2: ");
            for (int y = 0; y < arr2.GetLength(1); y++)
            {
                for (int x = 0; x < arr2.GetLength(0); x++)
                {
                    arr2[x, y] = 2;
                    Console.Write(arr2[x, y] + "| ");
                }
                Console.WriteLine();
            }

            matrix1.Dimension = arr1;
            matrix2.Dimension = arr2;

            SMatrix asd = matrix1 * matrix2;

            for (int y = 0; y < asd.Dimension.GetLength(1); y++)
            {
                for (int x = 0; x < asd.Dimension.GetLength(0); x++)
                {
                    Console.Write(asd.Dimension[x, y] + "|");
                }
                Console.WriteLine();
            }

            Assert.AreEqual(asd.Dimension.GetLength(0), 6);
            Assert.AreEqual(asd.Dimension.GetLength(1), 8);
            Assert.AreEqual(asd.Dimension[0, 0], 54);
        }
Пример #7
0
    /**
     * Static version of the normalize function. This time, it returns a full NEW SMatrix as a result.
     */
    public static SMatrix normalize(SMatrix a)
    {
        // Do not call the object's version because it changes the objects value
        float[,] result = new float[a.rows, a.cols];
        if (a.rows == a.cols && a.rows > 1 && a.cols > 1)
        {
            result = (a / a.det()).data;
        }
        else
        {
            if (a.cols == 1)
            {
                float square_sum = 0;
                for (int y = 0; y < a.rows; y++)
                {
                    square_sum += Mathf.Pow(a.data[y, 0], 2);
                }
                square_sum = Mathf.Sqrt(square_sum);
                result     = (a / square_sum).data;
            }
            else
            {
                for (int x = 0; x <  a.cols; x++)
                {
                    float square_sum = 0;
                    for (int y = 0; y < a.rows; y++)
                    {
                        square_sum += Mathf.Pow(a.data[y, x], 2);
                    }
                    square_sum = Mathf.Sqrt(square_sum);

                    for (int y = 0; y < a.rows; y++)
                    {
                        result[y, x] = (a.data[y, x] / square_sum);
                    }
                }
            }
        }
        return(new SMatrix(result));
    }
Пример #8
0
    /**
     * Allows SMatrices "subtraction" as an operation
     * Throws an exception if the formats do not match
     */
    public static SMatrix operator -(SMatrix a, SMatrix b)
    {
        if (a.cols == b.cols && a.rows == b.rows)
        {
            // Pass through every row and every column
            SMatrix result = new SMatrix(a.rows, b.cols);

            for (int row = 0; row < a.rows; row++)
            {
                for (int col = 0; col < a.cols; col++)
                {
                    result.set(row, col, (a.data[row, col] - b.data[row, col]));
                }
            }

            return(result);
        }
        else
        {
            throw new InvalidOperationException("Cannot perform addition on SMatrices of different sizes.");
        }
    }
Пример #9
0
    void Start()
    {
        // ----------------- Demonstrates the use of JSON_decode_file -----------------

        // Extract most structures of JSON without needing to make an Object class structure (somewhat dynamic)
        JSONNode_Result data_result = IO_Utils.JSON_decode_file("Assets/Unity_Utils/examples/example_json.txt");

        Debug.Log(data_result.correct);                     // Shows true if the file exists and is right
        Debug.Log(data_result.error);                       // Shows an empty string if no error

        Debug.Log(data_result.value["Random_Array"][0]);    // Displays an int (12)
        Debug.Log(data_result.value["Random_Array"][1]);    // Displays a string ("Apple")
        Debug.Log(data_result.value["Random_Array"][2]);    // Displays a float (0.25F)
        Debug.Log(data_result.value["Random_Array"][3][2]); // Displays a sub array element (2)

        // ----------------- List shuffling -----------------

        List <int> int_list = new List <int>()
        {
            1, 2, 3, 4, 5, 6, 7, 8, 9, 0
        };
        List <string> string_list = new List <string>()
        {
            "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"
        };

        // Directly shuffle the list and change its value
        int_list.shuffle();

        // Shuffle a list and get its value as a return without affecting the original list
        List <string> new_string_list = Data_Utils.shuffle_list(string_list);

        // ----------------- Demonstrates the use of SMatrix -----------------

        // How to create a matrix with a format but no direct values (All 0 for now)
        SMatrix a_matrix = new SMatrix(5, 5);

        // How to directly create a matrix with values
        a_matrix = new SMatrix(new float[5, 5] {
            { 2, 1, 5, -1, 6 },
            { 1, 4, 0, 3, 7 },
            { 1, 0, 0, -7, 9 },
            { 3, 2, 1, 0, 8 },
            { 1, 1, 0, 3, 4 }
        });

        SMatrix b_matrix = new SMatrix(new float[5, 5] {
            { 5, 3, 4, 2, -1 },
            { 2, 2, 3, 5, 5 },
            { 8, 10, 2, 36, 2 },
            { 7, 1, 1, 1, 3 },
            { 0, 3, 4, 8, 6 }
        });

        // Set a value of the SMatrix
        b_matrix.set(2, 2, 5F);

        // Shows simple operations for Matrices
        SMatrix add_matrix  = a_matrix + b_matrix;
        SMatrix sub_matrix  = a_matrix - b_matrix;
        SMatrix mult_matrix = a_matrix * b_matrix;

        // Multiply or divide the matrix with scalars. Note that for the division, the scalar comes after.
        SMatrix scalar_after_matrix  = a_matrix * 5F;
        SMatrix scalar_before_matrix = 5F * a_matrix;

        SMatrix scalar_divide_matrix = a_matrix / 5F;

        // Join the matrices into one
        SMatrix vert_matrix = SMatrix.join_vertical(a_matrix, b_matrix);
        SMatrix hor_matrix  = SMatrix.join_horizontal(a_matrix, b_matrix);

        // Directly transpose the matrix
        a_matrix.transpose();
        // Transpose a matrix without changing its values, but instead get it as a new matrix
        SMatrix transposed_a_matrix = SMatrix.transpose(a_matrix);

        // Directly normalize the matrix
        a_matrix.normalize();
        // Normalize a matrix without changing its values, but instead get it as a new matrix
        SMatrix normalized_a_matrix = SMatrix.normalize(a_matrix);

        // Get the determinant of the matrix if any
        float determinant = a_matrix.det();

        // Log the matrix in the console
        a_matrix.log();

        // Get the string representing the matrix and log it in the console
        string read = a_matrix.read();

        Debug.Log(read);
    }