Skip to content

Instantly share code, notes, and snippets.

@suatatan
Last active March 21, 2017 12:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save suatatan/86cc779dfbba7403cbb5478de4285fdc to your computer and use it in GitHub Desktop.
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