Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Aritmetical operations in array is impossible in C#. Using MathDot like algebraic libraries are known, however, it is not allowed in SQLServer CLR. To circumwent I've developed pure C# function to operate arrays.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LinearAlgebraExperiment
{
class Program
{
static void Main(string[] args)
{
float[] a = new float[] {10,20,30 };
float[] b = new float[] { 10, 20, 30 };
float[] c=multiply_array(a, b,"+");
Console.ReadLine();
}
/// <summary>
/// Multiplies array with single scalar
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="opsign"></param>
/// <returns></returns>
public static float[] array_multiple_scalar(float[] a, float b)
{
int i = 0;
float[] c = new float[a.Length];
while (i < a.Length)
{
float elma = 0;
elma = a[i];
c[i] = elma*b;
i = i + 1;
}
return (c);
}
/// <summary>
/// Operation with two array
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="opsign"></param>
/// <returns></returns>
public static float[] array_aritmetic(float[] a, float[] b, string opsign)
{
int i = 0;
float[] c = new float[a.Length];
while (i < a.Length)
{
Console.WriteLine(i);
float elma = a[i];
float elmb = b[i];
float elmc = 0;
if (opsign == "+")
{
elmc = elma + elmb;
}
else if (opsign == "-")
{
elmc = elma - elmb;
}
else if (opsign == "*")
{
elmc = elma * elmb;
}
c[i] = elmc;
i = i + 1;
}
return (c);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.