How to resolve the algorithm Element-wise operations step by step in the C# programming language
How to resolve the algorithm Element-wise operations step by step in the C# programming language
Table of Contents
Problem Statement
This task is similar to:
Implement basic element-wise matrix-matrix and scalar-matrix operations, which can be referred to in other, higher-order tasks. Implement:
Extend the task if necessary to include additional basic operations, which should not require their own specialised task.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Element-wise operations step by step in the C# programming language
The provided C# source code defines a static class ElementWiseOperations
and a program class Program
that demonstrates element-wise operations on 2D matrices. Here's a detailed explanation of the code:
-
ElementWiseOperations
Class:-
Private Fields and Dictionaries:
operations
is a private dictionary that maps operation names to delegate functions that perform element-wise operations. The supported operations are:- "add": addition
- "sub": subtraction
- "mul": multiplication
- "div": division
- "pow": exponentiation
-
DoOperation
Methods:- There are two
DoOperation
methods that perform element-wise operations on 2D matrices.- The first
DoOperation
method takes three parameters: the source matrixm
, the operation namename
, and another matrixother
. It performs the element-wise operation specified by the operation name on the two matrices and returns the result. - The second
DoOperation
method takes three parameters: the source matrixm
, the operation delegate functionoperation
, and another matrixother
. It performs the element-wise operation specified by the delegate function on the two matrices and returns the result.
- The first
- There are two
-
-
Program
Class:Main
Method:- Inside the
Main
method, a 3x4 matrixmatrix
and a 3x4 matrixtens
are defined.matrix
is shown on the console.- Element-wise addition is performed on
matrix
andtens
using theDoOperation
method with the "add" operation name. The result is printed on the console. - Element-wise subtraction is performed on
matrix
with a constant value of 100 using theDoOperation
method with a lambda expression. The result is printed on the console.
- Inside the
Example:
In the provided code, an example is given with a 3x4 matrix matrix
and a 3x4 matrix tens
. The element-wise addition of these two matrices is performed using the DoOperation
method with the "add" operation name. The result is a new 3x4 matrix where each element is the sum of the corresponding elements in matrix
and tens
.
Similarly, an element-wise subtraction is performed on matrix
with a constant value of 100 using a lambda expression delegate. The result is a new 3x4 matrix where each element is the difference between the corresponding element in matrix
and the constant value 100.
The output of the example would be:
[ 1, 2, 3, 4 ]
[ 5, 6, 7, 8 ]
[ 9, 10, 11, 12 ]
[ 11, 12, 13, 14 ]
[ 25, 26, 27, 28 ]
[ 39, 40, 41, 42 ]
[ -99, -98, -97, -96 ]
[ -95, -94, -93, -92 ]
[ -91, -90, -89, -88 ]
Source code in the csharp programming language
using System;
using System.Collections.Generic;
using System.Linq;
public static class ElementWiseOperations
{
private static readonly Dictionary<string, Func<double, double, double>> operations =
new Dictionary<string, Func<double, double, double>> {
{ "add", (a, b) => a + b },
{ "sub", (a, b) => a - b },
{ "mul", (a, b) => a * b },
{ "div", (a, b) => a / b },
{ "pow", (a, b) => Math.Pow(a, b) }
};
private static readonly Func<double, double, double> nothing = (a, b) => a;
public static double[,] DoOperation(this double[,] m, string name, double[,] other) =>
DoOperation(m, operations.TryGetValue(name, out var operation) ? operation : nothing, other);
public static double[,] DoOperation(this double[,] m, Func<double, double, double> operation, double[,] other) {
if (m == null || other == null) throw new ArgumentNullException();
int rows = m.GetLength(0), columns = m.GetLength(1);
if (rows != other.GetLength(0) || columns != other.GetLength(1)) {
throw new ArgumentException("Matrices have different dimensions.");
}
double[,] result = new double[rows, columns];
for (int r = 0; r < rows; r++) {
for (int c = 0; c < columns; c++) {
result[r, c] = operation(m[r, c], other[r, c]);
}
}
return result;
}
public static double[,] DoOperation(this double[,] m, string name, double number) =>
DoOperation(m, operations.TryGetValue(name, out var operation) ? operation : nothing, number);
public static double[,] DoOperation(this double[,] m, Func<double, double, double> operation, double number) {
if (m == null) throw new ArgumentNullException();
int rows = m.GetLength(0), columns = m.GetLength(1);
double[,] result = new double[rows, columns];
for (int r = 0; r < rows; r++) {
for (int c = 0; c < columns; c++) {
result[r, c] = operation(m[r, c], number);
}
}
return result;
}
public static void Print(this double[,] m) {
if (m == null) throw new ArgumentNullException();
int rows = m.GetLength(0), columns = m.GetLength(1);
for (int r = 0; r < rows; r++) {
Console.WriteLine("[ " + string.Join(", ", Enumerable.Range(0, columns).Select(c => m[r, c])) + " ]");
}
}
}
public class Program
{
public static void Main() {
double[,] matrix = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
double[,] tens = {
{ 10, 10, 10, 10 },
{ 20, 20, 20, 20 },
{ 30, 30, 30, 30 }
};
matrix.Print();
WriteLine();
(matrix = matrix.DoOperation("add", tens)).Print();
WriteLine();
matrix.DoOperation((a, b) => b - a, 100).Print();
}
}
You may also check:How to resolve the algorithm Day of the week step by step in the Scheme programming language
You may also check:How to resolve the algorithm Sorting algorithms/Quicksort step by step in the Python programming language
You may also check:How to resolve the algorithm Display a linear combination step by step in the Nim programming language
You may also check:How to resolve the algorithm Catalan numbers/Pascal's triangle step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Create an HTML table step by step in the Scala programming language