From d59812b0fb574f00b9d125cef50e53be3387e2f2 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 6 Mar 2025 14:32:10 -0600 Subject: [PATCH] start on moving lin alg functions over --- src/HushGP/Instructions/GenericInstructions.hs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/HushGP/Instructions/GenericInstructions.hs b/src/HushGP/Instructions/GenericInstructions.hs index b3ee48a..3e80180 100644 --- a/src/HushGP/Instructions/GenericInstructions.hs +++ b/src/HushGP/Instructions/GenericInstructions.hs @@ -1,13 +1,13 @@ module HushGP.Instructions.GenericInstructions where import Control.Lens -import HushGP.State -import HushGP.Instructions.Utility import Data.List (sort, sortBy) import Data.Ord import Data.List.Split - --- import Debug.Trace +import Numeric.LinearAlgebra (vector, norm_2) +import Numeric.Statistics.Moment +import HushGP.State +import HushGP.Instructions.Utility-- import Debug.Trace -- |Does No Operation. Useful for genome stuff :) instructionNoOpBlock :: State -> State @@ -564,3 +564,11 @@ instructionVectorInsertVector accessor state@(State {_int = i1 : is}) = state{_int = is} & accessor .~ (combineTupleList v2 (splitAt (fromIntegral i1) v1) : vs) _ -> state instructionVectorInsertVector _ state = state + +-- |Takes a numeric vector lens and a primitive lens. Pushes the mean of the top +-- vector to the primitive stack. +instructionVectorMean :: Fractional a => Lens' State [a] -> Lens' State [[a]] -> (b -> a) -> State -> State +instructionVectorMean primAccessor vectorAccessor wrangleFunc state = + case uncons (view vectorAccessor state) of + Just (v1, vs) -> state & vectorAccessor .~ vs & primAccessor .~ (mean v1 : view primAccessor state) + _ -> state