Basic Operations and Syntax

Your Progress in this Chapter

0%

0 / 12 units completed

Chapter Overview

Operators And Expressions

Section 1.3 of 615 min6 code examples

Collections and LINQ in C#

In numerical programming, we rarely work with single values. Collections allow us to group related data—such as a vector of residuals or a list of material properties. LINQ (Language Integrated Query) provides a powerful, declarative way to filter, transform, and analyze these collections.

1. Common Collection Types

C# provides several specialized collections. While arrays are the standard for fixed-size mathematical data, other collections offer dynamic resizing and key-based lookups.

TypeNamespaceCategoryDescription
Array (T[])SystemReference typeFixed-size, high-performance contiguous memory.
List<T>System.Collections.GenReference typeDynamically resizable array for iterative growth.
Dictionary<K,V>System.Collections.GenReference typeKey-value pairs for fast lookups.
HashSet<T>System.Collections.GenReference typeUnordered set of unique elements.

Example 1C#

1
2
3
4
5
6
7
8
9
10
11
Code is ready to run

2. Introduction to LINQ

LINQ allows you to perform query operations directly on collections. It simplifies tasks like finding the maximum error in a vector or extracting specific nodes from a mesh using a functional approach.

Example 2C#

1
2
3
4
5
6
7
8
9
10
11
12
13
Code is ready to run

3. Deferred Execution

A vital concept in LINQ is that queries are not executed when they are defined. They are executed when you "materialize" them (by using foreach, .ToArray(), or .ToList()). This is known as lazy evaluation.

Examples

.. Admonition:: Example 1 : Filtering Convergence Data

In this scenario, we have a list of error values captured from a solver that is struggling to reach a steady state. We want to programmatically extract only the "successful" steps—those where the error dropped below our predefined tolerance—to verify how often our algorithm performed well.

Example 3C#

1
2
3
4
5
6
Code is ready to run
OutputFrom the book
Found 2 converged steps.

.. Admonition:: Example 2 : Statistical Analysis

Imagine you have completed a simulation and possess a vector of residuals representing the difference between your guess and the true solution. Before proceeding, you need to calculate the average error to understand the general accuracy and the "Total Energy" (sum of squares) to assess the stability of the system.

Example 4C#

1
2
3
4
Code is ready to run
OutputFrom the book
Average: 0.038, Energy: 0.0103

.. Admonition:: Example 3 : Mapping Node IDs

In Finite Element Analysis (FEA), nodes are often assigned unique identification numbers that aren't necessarily sequential. By using a Dictionary, we can map these arbitrary Node IDs to their physical coordinates on a 1D beam, allowing for instant lookup without searching through a massive list.

Example 5C#

1
2
3
4
5
6
7
8
9
10
11
Code is ready to run
OutputFrom the book
Node 102 position: 0.5

.. Admonition:: Example 4 : Generating Sequences

Setup is a major part of numerical modeling. Instead of writing cumbersome loops to initialize an identity-like vector or an index map, we use LINQ generators to create a range of integers or a repeating set of initial guesses in a single line of code.

Example 6C#

1
2
3
4
5
Code is ready to run

Performance Note

While LINQ is expressive, it can introduce overhead due to allocations. For the "hot-path" of a numerical solver (like inner loops of matrix multiplication), traditional for-loops remain the preferred choice.