Extreme Optimization™: Complexity made simple.

Math and Statistics
Libraries for .NET

  • Home
  • Features
    • Math Library
    • Vector and Matrix Library
    • Statistics Library
    • Performance
    • Usability
  • Documentation
    • Introduction
    • Math Library User's Guide
    • Vector and Matrix Library User's Guide
    • Data Analysis Library User's Guide
    • Statistics Library User's Guide
    • Reference
  • Resources
    • Downloads
    • QuickStart Samples
    • Sample Applications
    • Frequently Asked Questions
    • Technical Support
  • Order
  • Company
    • About us
    • Testimonials
    • Customers
    • Press Releases
    • Careers
    • Partners
    • Contact us
Introduction
Deployment Guide
Nuget packages
Configuration
Using Parallelism
Expand Mathematics Library User's GuideMathematics Library User's Guide
Expand Vector and Matrix Library User's GuideVector and Matrix Library User's Guide
Expand Data Analysis Library User's GuideData Analysis Library User's Guide
Expand Statistics Library User's GuideStatistics Library User's Guide
Expand Data Access Library User's GuideData Access Library User's Guide
Expand ReferenceReference

Skip Navigation LinksHome»Documentation»Vector and Matrix Library User's Guide»Vectors»Vector Basics

Vector Basics

Extreme Optimization Numerical Libraries for .NET Professional

The VectorT class is an abstract base class that exposes the mathematical methods and properties of vectors. It also contains a default implementation for these methods and properties. A series of derived classes implement specific types of vectors. These are listed in the table below.

Class

Description

DenseVectorT

A standard vector. The elements can take on any value. This is the most common type.

ConstantVectorT

A vector whose elements are all equal to the same real number.

BlockVectorT

A vector that is made up of smaller vectors that may be of different types.

SparseVectorT

A vector that only stores the non-zero elements.

In addition to these, several internal vector classes are used to represent rows, columns and diagonals of matrices.

The VectorT class does not specify how vector elements should be stored. This is left entirely up to the derived classes. The derived vector types provide their own optimized implementations for many of the base methods and properties. Wherever possible, use is made of optimized implementations of the Basic Linear Algebra Subroutines (BLAS).

Some vector types put restrictions on the elements that can be changed. For example, you can't modify the elements of a ConstantVectorT at all. If you try to assign a value to a read-only element, an exception of type ComponentReadonlyException is thrown.

DenseVectorT can be used to represent any vector. It is the only vector type whose elements are all guaranteed to be writable.

Creating vectors

Vectors can be created by calling one of the factory methods of the Vector class. Most of these methods are overloaded.

Method

Description

Create

Constructs a new dense vector.

CreateConstantT

Constructs a new constant vector.

CreateSparse

Constructs a new sparse vector.

CreateCategorical

Constructs a new vector whose elements are restricted to a limited set of values (categories).

CreateBanded

Constructs a new vector that is nonzero only over part of the vector.

CreateRange

Constructs a new linearly spaced vector.

CreateLogarithmicRange

Constructs a new logarithmically spaced vector.

CreateRandom

Constructs a new vector that contains random values.

Copying vectors

There are three ways to make a copy of a vector.

The ShallowCopy method creates a derived vector that is a member-wise clone of the original. The new vector has the same type and length as the original vector. When you change the value of an element of this vector, the value of the corresponding element in the original vector changes as well, and vice versa.

The Clone method creates a stand-alone vector that is a complete copy of the original. This method creates a vector of the same type as the original, and makes its own copy of the element storage. When you change the value of an element of this vector, the value of the corresponding element in the original vector does not change. Likewise, if you change the value of an element of the original vector, the value of the corresponding element in the copied vector does not change.

The Clone method takes an optional parameter of type CloningMethod. This parameter specifies if you want to preserve the read-only elements of the vector. The possible values for this parameter are listed in the following table:

ValueDescriptionMakes an exact clone of the object.Makes a copy of the object such that all non-zero elements are writable.Makes a copy of the object such that all elements are writable.

You can convert a shallow copy to a full copy by calling the CloneData method. This method ensures that an instance has its own copy of the data. After a call to this method, any changes to the elements of this vector will only affect this instance.

C#
VB
C++
F#
Copy
// Create a vector, a shallow copy, and a clone:
Vector v = Vector.Create(1, 2, 3, 4, 5);
Vector vShallowCopy = v.ShallowCopy();
Vector vClone = v.Clone();
// This prints '2':
Console.WriteLine("v[1] = {0}", v[1]);
// Now change the value of the 2nd component.
v[1] = -1;
// Component changed. This prints '-1':
Console.WriteLine("v[1] = {0}", v[1]);
// Shallow copy changed, also. This prints '-1':
Console.WriteLine("vShallowCopy[1] = {0}", v[1]);
// Clone is left unchanged. This prints '2':
Console.WriteLine("vClone[1] = {0}", v[1]);
' Create a vector, a shallow copy, and a clone:
Vector v = Vector.Create(1, 2, 3, 4, 5)
Vector vShallowCopy = v.ShallowCopy()
Vector vClone = v.Clone()
' This prints '2':
Console.WriteLine("v(1) = {0}", v(1))
' Now change the value of the 2nd component.
v(1) = -1
' Component changed. This prints '-1':
Console.WriteLine("v(1) = {0}", v(1))
' Shallow copy changed, also. This prints '-1':
Console.WriteLine("vShallowCopy(1) = {0}", v(1))
' Clone is left unchanged. This prints '2':
Console.WriteLine("vClone(1) = {0}", v(1))

No code example is currently available or this language may not be supported.

No code example is currently available or this language may not be supported.

The third way to copy a vector is by calling the vector's ToDenseVector method. This method returns a stand-alone DenseVectorT whose elements are copied from the original vector. If this method is called on a DenseVectorT, a new vector is still created. In all cases, the elements of the new vector are guaranteed to be stored in a contiguous block of memory.

You can copy the elements of a vector to another, previously created vector using the CopyTo method. This method takes the second (destination) vector as its first and only argument. There is an overload that takes a second parameter, indicating the index in the destination where copying should begin. Care should be taken that you don't try to modify elements that are read-only. Any such attempt will result in a ComponentReadOnlyException.

Finally, the SwapT method swaps the elements between two vectors.

Copyright (c) 2004-2023 ExoAnalytics Inc.

Send comments on this topic to support@extremeoptimization.com

Copyright © 2004-2023, Extreme Optimization. All rights reserved.
Extreme Optimization, Complexity made simple, M#, and M Sharp are trademarks of ExoAnalytics Inc.
Microsoft, Visual C#, Visual Basic, Visual Studio, Visual Studio.NET, and the Optimized for Visual Studio logo
are registered trademarks of Microsoft Corporation.