Console.WriteLine("Please select an operation:");
Console.WriteLine("1. Dot product between two 3x1 vectors");
Console.WriteLine("2. Calculate cross product between two 3x1 vectors");
Console.WriteLine("3. Calculate trace of a matrix (2x2 or 3x3)");
Console.WriteLine("4. Calculate transpose of a matrix");
Console.WriteLine("5. Exit");
Console.Write("Enter your choice: ");
int choice = int.Parse(Console.ReadLine());
double[] vector1 = ReadVector(3);
double[] vector2 = ReadVector(3);
double dotProduct = CalculateDotProduct(ref vector1, ref vector2);
Console.WriteLine($"Dot Product: {dotProduct}");
double[] vectorA = ReadVector(3);
double[] vectorB = ReadVector(3);
double[] crossProduct = CalculateCrossProduct(ref vectorA, ref vectorB);
Console.WriteLine($"Cross Product: [{crossProduct[0]}, {crossProduct[1]}, {crossProduct[2]}]");
Console.Write("Enter square matrix size (ex: [2] for 2x2 matrix): ");
int size = int.Parse(Console.ReadLine());
double[,] matrix = ReadMatrix(size);
double trace = CalculateTrace(matrix);
Console.WriteLine($"Trace: {trace}");
double[,] inputMatrix = ReadMatrix();
double[,] transposeMatrix = CalculateTranspose(inputMatrix);
Console.WriteLine("Transposed Matrix:");
PrintMatrix(transposeMatrix);
Console.WriteLine("Invalid choice. Please try again.");
static double[] ReadVector(int size)
double[] vector = new double[size];
Console.WriteLine($"Enter {size} elements for the vector:");
for (int i = 0; i < size; i++)
Console.Write($"Element {i}: ");
vector[i] = double.Parse(Console.ReadLine());
static double[,] ReadMatrix(int size = 3)
double[,] matrix = new double[size, size];
Console.WriteLine($"Enter {size}x{size} matrix elements:");
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
Console.Write($"Element [{i},{j}]: ");
matrix[i, j] = double.Parse(Console.ReadLine());
static double CalculateDotProduct(ref double[] vector1, ref double[] vector2)
for (int i = 0; i < vector1.Length; i++)
result += vector1[i] * vector2[i];
static double[] CalculateCrossProduct(ref double[] vectorA, ref double[] vectorB)
double[] crossProduct = new double[3];
crossProduct[0] = vectorA[1] * vectorB[2] - vectorA[2] * vectorB[1];
crossProduct[1] = vectorA[2] * vectorB[0] - vectorA[0] * vectorB[2];
crossProduct[2] = vectorA[0] * vectorB[1] - vectorA[1] * vectorB[0];
static double CalculateTrace(double[,] matrix)
for (int i = 0; i < matrix.GetLength(0); i++)
static double[,] CalculateTranspose(double[,] matrix)
int rows = matrix.GetLength(0);
int columns = matrix.GetLength(1);
double[,] transposeMatrix = new double[columns, rows];
for (int i = 0; i < columns; i++)
for (int j = 0; j < rows; j++)
transposeMatrix[i, j] = matrix[j, i];
static void PrintMatrix(double[,] matrix)
int rows = matrix.GetLength(0);
int columns = matrix.GetLength(1);
for (int i = 0; i < rows; i++)
for (int j = 0; j < columns; j++)
Console.Write(matrix[i, j] + " ");