Jacobi.ArrayOperators 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Jacobi.ArrayOperators --version 1.1.0
                    
NuGet\Install-Package Jacobi.ArrayOperators -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Jacobi.ArrayOperators" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Jacobi.ArrayOperators" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Jacobi.ArrayOperators" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Jacobi.ArrayOperators --version 1.1.0
                    
#r "nuget: Jacobi.ArrayOperators, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Jacobi.ArrayOperators@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Jacobi.ArrayOperators&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Jacobi.ArrayOperators&version=1.1.0
                    
Install as a Cake Tool

Array Operators

This library provides a set of operator extension for Array, IEnumerable<T> and ICollection<T> types.

This will allow you to do 'array arithmetic' in a more natural way.

Key Advantages

  1. Readability: Mathematical operations look like actual math notation
  2. Performance: Batch operations instead of loops
  3. Maintainability: Less boilerplate code
  4. Safety: Type-safe generic constraints ensure valid operations
  5. Expressiveness: Natural syntax for array/vector mathematics

Usage

Add the Jacobi.ArrayOperators nuget package to your project.

using Jacobi.ArrayOperators;

Here are some examples of array (IEnumerable<T>) arithmetic:

int[] arr = [1, 2, 3];
// this works for any IEnumerable<T>
var result = arr * 5;
// result is [5, 10, 15]
int[] arr1 = [1, 2, 3];
int[] arr2 = [6, 8, 3];
var result = arr + arr2;
// result is [7, 10, 6]

Array specific operations:

int[] arr = [1, 2, 3];
arr += 5;
// arr is now [6, 7, 8]

When Arrays are of different lengths, only the calculated items are returned (shortest array length):

IEnumerable<int> arr1 = [1, 2, 3];
IEnumerable<int> arr2 = [4, 5, 6, 7];
var result = arr1 + arr2;
// result is [5, 7, 9]

Compare two arrays (IEnumerable<T>):

int[] arr1 = [1, 2, 3];
int[] arr2 = [1, 2, 3];
if (arr1 == arr2)
{
    // this block will be executed
}
int[] arr1 = [1, 2, 3]
int[] arr2 = [6, 8, 3]
if (arr1 > arr2)
{
    // this block will not be executed
}

Operator Extensions Reference

IEnumerable<T> Arithmetic Operators (Element-wise)

Operator Description Example Result
+ Element-wise addition of two sequences [1, 2, 3] + [4, 5, 6] [5, 7, 9]
- Element-wise subtraction of two sequences [10, 20, 30] - [1, 2, 3] [9, 18, 27]
* Element-wise multiplication of two sequences [1, 2, 3] * [4, 5, 6] [4, 10, 18]
/ Element-wise division of two sequences [10, 20, 30] / [2, 4, 5] [5, 5, 6]
% Element-wise modulus of two sequences [10, 20, 30] % [3, 6, 7] [1, 2, 2]
- (unary) Negates each element -[1, -2, 3] [-1, 2, -3]
+ (unary) Unary plus on each element +[1, 2, 3] [1, 2, 3]

IEnumerable<T> Scalar Arithmetic Operators

Operator Description Example Result
+ Adds scalar to each element [1, 2, 3] + 5 [6, 7, 8]
- Subtracts scalar from each element [10, 20, 30] - 5 [5, 15, 25]
* Multiplies each element by scalar [1, 2, 3] * 5 [5, 10, 15]
/ Divides each element by scalar [10, 20, 30] / 5 [2, 4, 6]
% Modulus of each element by scalar [11, 12, 13] % 5 [1, 2, 3]

IEnumerable<bool> Logical Operators

Operator Description Example Result
& Element-wise logical AND [true, false, true] & [true, true, false] [true, false, false]
\| Element-wise logical OR [true, false, false] \| [false, false, true] [true, false, true]
^ Element-wise logical XOR [true, false, true] ^ [true, true, false] [false, true, true]
! Negates each boolean element ![true, false, true] [false, true, false]
true Returns true if all elements are true if ([true, true, true]) true
false Returns true if all elements are false if (![false, false, false]) true

IEnumerable<T> Bitwise Operators

Operator Description Example Result
& Element-wise bitwise AND [0b1100, 0b1010] & [0b1010, 0b1100] [0b1000, 0b1000]
\| Element-wise bitwise OR [0b1100, 0b1010] \| [0b0011, 0b0101] [0b1111, 0b1111]
^ Element-wise bitwise XOR [0b1100, 0b1010] ^ [0b1010, 0b1100] [0b0110, 0b0110]
~ Bitwise complement of each element ~[0b1100, 0b0011] [~0b1100, ~0b0011]
<< Left shift each element by amount [1, 2, 3] << 2 [4, 8, 12]
>> Right shift each element by amount [16, 32, 64] >> 2 [4, 8, 16]
>>> Unsigned right shift each element [8, 16, 32] >>> 1 [4, 8, 16]

IEnumerable<T> Comparison Operators

Operator Description Example Result
== Compares sequences for equality [1, 2, 3] == [1, 2, 3] true
!= Compares sequences for inequality [1, 2, 3] != [1, 2, 4] true
< Lexicographic less than comparison [1, 2, 3] < [1, 2, 4] true
> Lexicographic greater than comparison [1, 3] > [1, 2, 3] true
<= Lexicographic less than or equal [1, 2] <= [1, 2, 3] true
>= Lexicographic greater than or equal [1, 2, 3] >= [1, 2] true

T[] Array Arithmetic Operators (In-place)

Operator Description Example Result
++ Increments each element in-place arr++ where arr = [1, 2, 3] [2, 3, 4]
-- Decrements each element in-place arr-- where arr = [1, 2, 3] [0, 1, 2]
+= Adds scalar to each element in-place arr += 5 where arr = [1, 2, 3] [6, 7, 8]
-= Subtracts scalar from each element arr -= 5 where arr = [10, 20] [5, 15]
*= Multiplies each element by scalar arr *= 5 where arr = [1, 2, 3] [5, 10, 15]
/= Divides each element by scalar arr /= 2 where arr = [10, 20] [5, 10]
%= Modulus each element by scalar arr %= 5 where arr = [11, 12] [1, 2]

IList<T> Arithmetic Operators (In-place)

Operator Description Example Result
++ Increments each element in-place list++ where list = [1, 2, 3] [2, 3, 4]
-- Decrements each element in-place list-- where list = [1, 2, 3] [0, 1, 2]
+= Adds scalar to each element in-place list += 5 where list = [1, 2] [6, 7]
-= Subtracts scalar from each element list -= 3 where list = [10, 20] [7, 17]
*= Multiplies each element by scalar list *= 2 where list = [5, 10] [10, 20]
/= Divides each element by scalar list /= 2 where list = [10, 20] [5, 10]
%= Modulus each element by scalar list %= 5 where list = [11, 12] [1, 2]

ICollection<T> Arithmetic Operators (In-place)

Operator Description Example Result
++ Increments each element in-place col++ where col = [1, 2, 3] [2, 3, 4]
-- Decrements each element in-place col-- where col = [1, 2, 3] [0, 1, 2]
+= Adds scalar to each element in-place col += 5 where col = [1, 2] [6, 7]
-= Subtracts scalar from each element col -= 3 where col = [10, 20] [7, 17]
*= Multiplies each element by scalar col *= 2 where col = [5, 10] [10, 20]
/= Divides each element by scalar col /= 2 where col = [10, 20] [5, 10]
%= Modulus each element by scalar col %= 5 where col = [11, 12] [1, 2]

BitArray Operators

Operator Description Example Result
& Bitwise AND of two BitArrays bitArr1 & bitArr2 Element-wise AND
\| Bitwise OR of two BitArrays bitArr1 \| bitArr2 Element-wise OR
^ Bitwise XOR of two BitArrays bitArr1 ^ bitArr2 Element-wise XOR
! Bitwise NOT (inverts all bits) !bitArr Inverted bits
true Returns true if all bits are set if (bitArr) true if all 1s
false Returns true if all bits are clear if (!bitArr) true if all 0s

BitVector32 Operators

Operator Description Example Result
& Bitwise AND vec1 & vec2 Bitwise AND result
\| Bitwise OR vec1 \| vec2 Bitwise OR result
^ Bitwise XOR vec1 ^ vec2 Bitwise XOR result
! Bitwise NOT (complement) !vec Bitwise complement
true Returns true if all bits are set if (vec) true if Data == int.MaxValue
false Returns true if no bits are set if (!vec) true if Data == 0

Notes:

  • Element-wise operations stop at the length of the shorter sequence
  • In-place operators modify the original array/collection/list
  • IList<T> operators use optimized indexed access
  • ICollection<T> operators work with non-indexed collections (e.g., HashSet<T>)
  • Requires types implementing appropriate System.Numerics interfaces
  • BitArray operations maintain minimum length between operands
  • BitVector32 works with 32-bit integer data

Method Extensions Reference

IEnumerable<T> where T : IFloatingPoint<T>

Method Description Example Result
Abs() Returns absolute value of each element [-1.5, 2.3, -3.7].Abs() [1.5, 2.3, 3.7]
Floor() Rounds each element down to nearest integer [1.7, 2.3, 3.9].Floor() [1.0, 2.0, 3.0]
Ceiling() Rounds each element up to nearest integer [1.1, 2.5, 3.2].Ceiling() [2.0, 3.0, 4.0]
Round() Rounds each element to nearest integer [1.4, 2.5, 3.6].Round() [1.0, 2.0, 4.0]

IEnumerable<T> where T : INumber<T>

Method Description Example Result
Sum() Calculates sum of all elements [1, 2, 3, 4].Sum() 10
Product() Calculates product of all elements [2, 3, 4].Product() 24
Average() Calculates average of all elements [10, 20, 30].Average() 20

Potential Use Cases

The Jacobi.ArrayOperators library enables powerful array-based computations across various domains. Here are practical use cases:

1. Scientific Computing & Data Analysis

Statistical Calculations
// Normalize data (z-score normalization)
double[] data = [10.5, 12.3, 9.8, 11.2, 13.1];
double mean = data.Average();
double stdDev = CalculateStdDev(data);
var normalized = (data - mean) / stdDev;
Vector Operations
// Calculate dot product using element-wise multiplication
double[] vector1 = [1.0, 2.0, 3.0];
double[] vector2 = [4.0, 5.0, 6.0];
var dotProduct = (vector1 * vector2).Sum(); // 32.0

2. Image Processing

Brightness Adjustment
// Increase brightness by 20 units across all pixels
byte[] pixels = GetImagePixels();
pixels += 20;
Image Blending
// Blend two images with 50% opacity each
byte[] image1 = GetImage1Pixels();
byte[] image2 = GetImage2Pixels();
var blended = (image1 + image2) / 2;
Apply Masks
// Apply binary mask to filter pixels
bool[] mask = GetMask();
byte[] pixels = GetPixels();
var filtered = mask ? pixels : default; // Using true operator

3. Signal Processing

Apply Gain/Attenuation
// Amplify audio signal by 2x
float[] audioSamples = LoadAudioData();
audioSamples *= 2.0f;
Signal Mixing
// Mix two audio channels
float[] leftChannel = GetLeftChannel();
float[] rightChannel = GetRightChannel();
var mixedMono = (leftChannel + rightChannel) / 2.0f;

4. Financial Calculations

Portfolio Analysis
// Calculate daily returns
decimal[] prices = [100m, 102m, 101m, 105m];
decimal[] previousPrices = [99m, 100m, 102m, 101m];
var returns = (prices - previousPrices) / previousPrices * 100;
Risk-Weighted Assets
// Apply risk weights to asset values
decimal[] assetValues = [1000m, 2000m, 1500m];
decimal[] riskWeights = [0.5m, 0.75m, 1.0m];
var riskWeightedAssets = assetValues * riskWeights;

5. Game Development

Bulk Transform Operations
// Scale all enemy positions
float[] enemyPositions = [10.0f, 20.0f, 30.0f];
enemyPositions *= scaleFactor;
Damage Calculations
// Apply armor reduction to damage values
int[] damageValues = [50, 75, 100];
float[] armorReduction = [0.8f, 0.8f, 0.8f];
var finalDamage = damageValues * armorReduction;
Batch Health Updates
// Apply healing to multiple entities
int[] healthValues = [50, 75, 30];
healthValues += healingAmount;

6. Machine Learning & AI

Feature Scaling
// Min-max normalization
double[] features = [100, 200, 150, 300];
double min = features.Min();
double max = features.Max();
var scaled = (features - min) / (max - min);
Activation Functions
// Apply ReLU activation (using comparison)
double[] values = [-1.0, 2.0, -3.0, 4.0];
var activated = values > 0 ? values : 0; // Simplified concept
Gradient Descent Updates
// Update weights
double[] weights = [0.5, 0.3, 0.8];
double learningRate = 0.01;
double[] gradients = [0.1, -0.2, 0.15];
weights -= gradients * learningRate;

7. Physics Simulations

Force Calculations
// F = m * a
double[] masses = [10.0, 15.0, 20.0];
double[] accelerations = [2.0, 1.5, 3.0];
var forces = masses * accelerations;
Velocity Updates
// Update velocities with acceleration
double[] velocities = [5.0, 10.0, 15.0];
double deltaTime = 0.016; // ~60 FPS
velocities += accelerations * deltaTime;

8. Bitwise Operations for Flags/Permissions

Combine Permission Sets
// Merge permission flags
int[] userPermissions = [0b0001, 0b0010, 0b0100];
int[] rolePermissions = [0b0010, 0b0100, 0b1000];
var combinedPermissions = userPermissions | rolePermissions;
Apply Masks
// Extract specific bits
int[] values = [0b11111111, 0b10101010, 0b11110000];
int mask = 0b00001111;
var masked = values & mask;

9. Data Validation & Filtering

Boolean Mask Operations
// Check if all values meet criteria
bool[] validations = CheckDataValidity();
if (validations) // Uses true operator
{
    Console.WriteLine("All data is valid!");
}
Logical Combinations
// Combine multiple validation results
bool[] check1 = [true, false, true];
bool[] check2 = [true, true, false];
var allPassed = check1 & check2; // [true, false, false]

10. Time Series Analysis

Moving Averages
// Simple moving average calculation
double[] window1 = GetWindow(data, 0, 5);
double[] window2 = GetWindow(data, 1, 5);
var difference = window2 - window1;
Percentage Changes
// Calculate period-over-period changes
decimal[] currentPeriod = [100m, 110m, 105m];
decimal[] previousPeriod = [95m, 105m, 100m];
var percentChange = ((currentPeriod - previousPeriod) / previousPeriod) * 100;
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.1 175 11/15/2025
1.1.0 290 11/13/2025
1.0.0 294 11/12/2025