Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Cortex.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.10.34607.79
Expand Down Expand Up @@ -57,6 +56,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cortex.Streams.Elasticsearc
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cortex.Types", "src\Cortex.Types\Cortex.Types.csproj", "{64E12D4C-FBB2-4004-8316-C886CBFC614B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cortex.Vectors", "src\Cortex.Vectors\Cortex.Vectors.csproj", "{268BA5C7-C6FB-4A6B-875A-492659ED4573}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cortex.Mediator.Behaviors.FluentValidation", "src\Cortex.Mediator.Behaviors.FluentValidation\Cortex.Mediator.Behaviors.FluentValidation.csproj", "{44A166BD-01E9-4A4B-9BC5-7DE01B472E73}"
EndProject
Global
Expand Down Expand Up @@ -172,6 +173,10 @@ Global
{64E12D4C-FBB2-4004-8316-C886CBFC614B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{64E12D4C-FBB2-4004-8316-C886CBFC614B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{64E12D4C-FBB2-4004-8316-C886CBFC614B}.Release|Any CPU.Build.0 = Release|Any CPU
{268BA5C7-C6FB-4A6B-875A-492659ED4573}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{268BA5C7-C6FB-4A6B-875A-492659ED4573}.Debug|Any CPU.Build.0 = Debug|Any CPU
{268BA5C7-C6FB-4A6B-875A-492659ED4573}.Release|Any CPU.ActiveCfg = Release|Any CPU
{268BA5C7-C6FB-4A6B-875A-492659ED4573}.Release|Any CPU.Build.0 = Release|Any CPU
{44A166BD-01E9-4A4B-9BC5-7DE01B472E73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{44A166BD-01E9-4A4B-9BC5-7DE01B472E73}.Debug|Any CPU.Build.0 = Debug|Any CPU
{44A166BD-01E9-4A4B-9BC5-7DE01B472E73}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
26 changes: 26 additions & 0 deletions src/Cortex.Vectors/Abstractions/IVector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using System.Numerics;

namespace Cortex.Vectors
{
/// <summary>
/// Common contract for vector collections.
/// Provides dimension metadata and core linear‑algebra operations.
/// </summary>
/// <typeparam name="T">Any IEEE‑754 floating‑point numeric type (float, double, Half, decimal).</typeparam>
public interface IVector<T> : IReadOnlyList<T> where T : IFloatingPointIeee754<T>
{
/// <summary>Gets the number of components in the vector.</summary>
int Dimension { get; }

/// <summary>Dot (inner) product with another vector.</summary>
/// <exception cref="ArgumentException">Thrown when vector dimensions differ.</exception>
T Dot(IVector<T> other);

/// <summary>Euclidean norm (L2).</summary>
T Norm();

/// <summary>Returns a unit‑length copy of this vector.</summary>
IVector<T> Normalize();
}
}
Binary file added src/Cortex.Vectors/Assets/andyX.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions src/Cortex.Vectors/Assets/license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2025 Buildersoft

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
124 changes: 124 additions & 0 deletions src/Cortex.Vectors/BitVector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;

namespace Cortex.Vectors
{
/// <summary>
/// Fixed‑length bit‑packed vector that implements <see cref="IVector{T}"/> for any IEEE‑754 type <typeparamref name="T"/>.
/// Each bit encodes 0 → <see cref="T.Zero"/>, 1 → <see cref="T.One"/>.
/// </summary>
public sealed class BitVector<T> : IVector<T>, IEquatable<BitVector<T>> where T : IFloatingPointIeee754<T>
{
private readonly ulong[] _blocks;

public int Dimension { get; }
public int Count => Dimension;

#region Construction
public BitVector(int dimension)
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(dimension);
Dimension = dimension;
_blocks = new ulong[(dimension + 63) >> 6];
}

/// <summary>Create from indices that should be set to 1.</summary>
public BitVector(int dimension, IEnumerable<int> oneIndices) : this(dimension)
{
foreach (var idx in oneIndices) SetBit(idx, true);
}

/// <summary>Create from a span of bools.</summary>
public BitVector(ReadOnlySpan<bool> bits) : this(bits.Length)
{
for (int i = 0; i < bits.Length; i++) if (bits[i]) SetBit(i, true);
}
#endregion

#region Bit helpers
[MethodImpl(MethodImplOptions.AggressiveInlining)] private static (int blk, int off) Loc(int idx) => (idx >> 6, idx & 63);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool GetBit(int idx)
{
if ((uint)idx >= Dimension) throw new IndexOutOfRangeException();
var (b, o) = Loc(idx);
return (_blocks[b] & (1UL << o)) != 0UL;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetBit(int idx, bool value)
{
if ((uint)idx >= Dimension) throw new IndexOutOfRangeException();
var (b, o) = Loc(idx);
if (value) _blocks[b] |= 1UL << o; else _blocks[b] &= ~(1UL << o);
}
public int PopCount()
{
int c = 0; foreach (var v in _blocks) c += BitOperations.PopCount(v); return c;
}
#endregion

#region IVector Implementation
public T this[int index]
{
get => GetBit(index) ? T.One : T.Zero;
}

public T Dot(IVector<T> other)
{
if (other is BitVector<T> bv)
{
ValidateSameDimension(bv);
int count = 0;
for (int i = 0; i < _blocks.Length; i++) count += BitOperations.PopCount(_blocks[i] & bv._blocks[i]);
return T.CreateTruncating(count);
}
else
{
ValidateSameDimension(other);
T sum = T.Zero;
for (int i = 0; i < Dimension; i++) sum += this[i] * other[i];
return sum;
}
}

public T Norm() => T.Sqrt(T.CreateTruncating(PopCount()));

public IVector<T> Normalize()
{
var n = Norm();
if (n == T.Zero) throw new InvalidOperationException("Cannot normalize zero bit‑vector.");
var inv = T.One / n;
var data = new T[Dimension];
for (int i = 0; i < Dimension; i++) if (GetBit(i)) data[i] = inv; // zeros already default
return new DenseVector<T>(data);
}
#endregion

#region IEnumerable
public IEnumerator<T> GetEnumerator()
{
for (int i = 0; i < Dimension; i++) yield return this[i];
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
#endregion

#region Equality & HashCode
public bool Equals(BitVector<T>? other)
{
if (other is null || other.Dimension != Dimension) return false;
for (int i = 0; i < _blocks.Length; i++) if (_blocks[i] != other._blocks[i]) return false;
return true;
}
public override bool Equals(object? obj) => obj is BitVector<T> bv && Equals(bv);
public override int GetHashCode() => HashCode.Combine(Dimension, _blocks.Length > 0 ? _blocks[0] : 0UL);
#endregion

private void ValidateSameDimension(IVector<T> other)
{
if (other.Dimension != Dimension) throw new ArgumentException("Vector dimensions must match.", nameof(other));
}
}
}
59 changes: 59 additions & 0 deletions src/Cortex.Vectors/Cortex.Vectors.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>

<AssemblyVersion>2.0.0</AssemblyVersion>
<FileVersion>2.0.0</FileVersion>
<Product>Buildersoft Cortex Framework</Product>
<Company>Buildersoft</Company>
<Authors>Buildersoft,EnesHoxha</Authors>
<Copyright>Copyright © Buildersoft 2025</Copyright>

<Description>Cortex Data Framework is a robust, extensible platform designed to facilitate real-time data streaming, processing, and state management. It provides developers with a comprehensive suite of tools and libraries to build scalable, high-performance data pipelines tailored to diverse use cases. By abstracting underlying streaming technologies and state management solutions, Cortex Data Framework enables seamless integration, simplified development workflows, and enhanced maintainability for complex data-driven applications. </Description>


<RepositoryUrl>https://github.com/buildersoftio/cortex</RepositoryUrl>
<PackageTags>cortex;machine‑learning;vector;ai;streaming</PackageTags>

<Version>2.0.0</Version>
<PackageLicenseFile>license.md</PackageLicenseFile>
<PackageIcon>andyX.png</PackageIcon>
<PackageId>Cortex.Vectors</PackageId>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<IsPublishable>True</IsPublishable>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>Just as the Cortex in our brains handles complex processing efficiently, Cortex Data Framework brings brainpower to your data management! </PackageReleaseNotes>
<PackageProjectUrl>https://buildersoft.io/</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>

</PropertyGroup>

<ItemGroup>
<None Remove="README.md" />
</ItemGroup>

<ItemGroup>
<Content Include="README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<None Include="Assets\andyX.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
<None Include="Assets\license.md">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<Folder Include="Assets\" />
</ItemGroup>

</Project>
Loading