Skip to content

Instantly share code, notes, and snippets.

@Typiqally
Created December 5, 2022 13:34
Show Gist options
  • Save Typiqally/6f81dc70676557df6d7aefe0bbdd1c0e to your computer and use it in GitHub Desktop.
Save Typiqally/6f81dc70676557df6d7aefe0bbdd1c0e to your computer and use it in GitHub Desktop.
Hadamard product implementation using simd matrices written in Swift
//
// PlantMorphology
//
// Created by Jelle Maas on 05/12/2022.
//
import Foundation
import simd
// Extension functions for vDSP implementing the hadamard product operation for matrices
// see https://en.wikipedia.org/wiki/Hadamard_product_(matrices) for more info
func simd_hadamardProduct(a: simd_float2x2, b: simd_float2x2) -> simd_float2x2 {
return simd_float2x2([
a[0] * b[0],
a[1] * b[1]
])
}
func simd_hadamardProduct(a: simd_float3x3, b: simd_float3x3) -> simd_float3x3 {
return simd_float3x3([
a[0] * b[0],
a[1] * b[1],
a[2] * b[2]
])
}
func simd_hadamardProduct(a: simd_float4x4, b: simd_float4x4) -> simd_float4x4 {
return simd_float4x4([
a[0] * b[0],
a[1] * b[1],
a[2] * b[2],
a[3] * b[3]
])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment