KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Vector Utility Class for Financial and Statistical Calculations – Part 1
PRODUCT: 4D | VERSION: 21 | PLATFORM: Mac & Win
Published On: March 12, 2026
The 4D.Vector class offers efficient tools for high-dimensional numeric computations, but applications often embed custom math operations (e.g., mean, variance, normalization) within domain-specific code, leading to duplication and harder maintenance.

cs.VectorUtility
property defaultDimension : Integer

Class constructor
    This.defaultDimension:=3 // Simple default for examples. Adjust as needed

Function createZeroVector($dimension : Integer) : 4D.Vector
    If (Count parameters < 1)
        $dimension:=This.defaultDimension
    End if
    If ($dimension <= 0)
        return 4D.Vector.new(New collection)
    End if

    var $components : Collection
    $components:=New collection.resize($dimension; 0)
    return 4D.Vector.new($components)

Function addVectors($vecA : 4D.Vector$vecB : .Vector) : .Vector
    If ($vecA.length # $vecB.length)
        return This.createZeroVector(0)
    End if
    var $components : Collection
    $components:=New collection.resize($vecA.length)
    var $index : Integer
    For ($index; 0; $vecA.length - 1)
        $components[$index]:=$vecA[$index] + $vecB[$index]
    End for
    return 4D.Vector.new($components)

Function multiplyByScalar($vector : .Vector$multiplier : Real) : .Vector
    var $components : Collection
    $components:=New collection.resize($vector.length)
    var $index : Integer
    For ($index; 0; $vector.length - 1)
        $components[$index]:=$vector[$index] * $multiplier
    End for
    return 4D.Vector.new($components)


Example: Aggregating Client KPI Vectors

var $utility : cs.VectorUtility
$utility:=cs.VectorUtility.new()
// Sample KPIs: [revenue, expenses, margin] for Q1-Q4
var $q1 : 4D.Vector
$q1:=4D.Vector.new([100000; 60000; 0.4])
var $q2 : 4D.Vector
$q2:=4D.Vector.new([120000; 70000; 0.416])
var $q3 : 4D.Vector
$q3:=4D.Vector.new([110000; 65000; 0.409])
var $q4 : 4D.Vector
$q4:=4D.Vector.new([130000; 75000; 0.423])

var $annual : 4D.Vector
$annual:=$utility.addVectors($utility.addVectors($utility.addVectors($q1$q2); $q3); $q4)

var $avgQuarter : 4D.Vector
$avgQuarter:=$utility.multiplyByScalar($annual; 0.25)

ALERT("Annual KPIs: " + JSON Stringify($annual.toCollection())