Harambe
Harambe
MModular
Created by Harambe on 2/14/2024 in #questions
Different outputs with random numbers
Hi, I am running code to find the covariance of a matrix from scratch. When the code is run I sometimes get large or small random unexpected numbers or even infinity. It seems to change every time the code is run even when there is no random element in the code, and should produce the same output. Many thanks for any advice. Code snippet:
fn CovarienceMatrix(borrowed self) -> Matrix:
let transposed_matrix = self.Transpose()
var avg_dataset = Matrix(transposed_matrix.height, 1, Float32(0))
var varience = Matrix(transposed_matrix.height, 1, Float32(0))
var covarince_matrix = Matrix(transposed_matrix.height, transposed_matrix.height, Float32(0))
for i in range(transposed_matrix.height):
@parameter
fn sum_total[num_elmts: Int](j: Int):
avg_dataset[i,0] = avg_dataset[i,0] + transposed_matrix.load[num_elmts](i,j).reduce_add[1]()
vectorize[simd_width, sum_total](transposed_matrix.width)
avg_dataset[i,0] = avg_dataset[i,0] / transposed_matrix.width
for i in range(transposed_matrix.height):
@parameter
fn normalise_total[num_elmts: Int](j: Int):
varience[i,0] = varience[i,0] + pow(transposed_matrix.load[num_elmts](i,j) - avg_dataset[i,0], 2).reduce_add[1]()
vectorize[simd_width, normalise_total](transposed_matrix.width)
varience[i,0] = sqrt(varience[i,0] / transposed_matrix.width)
for i in range(transposed_matrix.height):
for j in range(transposed_matrix.height):
covarince_matrix[i, j] = varience[i,0] * varience[j,0]
return covarince_matrix
fn CovarienceMatrix(borrowed self) -> Matrix:
let transposed_matrix = self.Transpose()
var avg_dataset = Matrix(transposed_matrix.height, 1, Float32(0))
var varience = Matrix(transposed_matrix.height, 1, Float32(0))
var covarince_matrix = Matrix(transposed_matrix.height, transposed_matrix.height, Float32(0))
for i in range(transposed_matrix.height):
@parameter
fn sum_total[num_elmts: Int](j: Int):
avg_dataset[i,0] = avg_dataset[i,0] + transposed_matrix.load[num_elmts](i,j).reduce_add[1]()
vectorize[simd_width, sum_total](transposed_matrix.width)
avg_dataset[i,0] = avg_dataset[i,0] / transposed_matrix.width
for i in range(transposed_matrix.height):
@parameter
fn normalise_total[num_elmts: Int](j: Int):
varience[i,0] = varience[i,0] + pow(transposed_matrix.load[num_elmts](i,j) - avg_dataset[i,0], 2).reduce_add[1]()
vectorize[simd_width, normalise_total](transposed_matrix.width)
varience[i,0] = sqrt(varience[i,0] / transposed_matrix.width)
for i in range(transposed_matrix.height):
for j in range(transposed_matrix.height):
covarince_matrix[i, j] = varience[i,0] * varience[j,0]
return covarince_matrix
11 replies
MModular
Created by Harambe on 12/28/2023 in #questions
Can someone help me with this error please
Hi, I wanted to create mathematical functions for matrices (e.g addition, subtraction, multiplication, ect) However I am running into an error with the return statement in all of the operations. Many thanks for any advice. Error:
error: value of type 'Matrix' cannot be copied into its destination
return new_matirx
error: value of type 'Matrix' cannot be copied into its destination
return new_matirx
Code (with just addition function):
struct Matrix:
var height: Int
var width: Int
var total_items: Int
var data: Pointer[Float32]

fn __init__(inout self, height: Int, width: Int, default_value: Float32) -> None:
self.height = height
self.width = width
self.total_items = height * width
self.data = Pointer[Float32].alloc(self.total_items)
for item in range(self.total_items):
self.data.store(item, default_value)

fn __setitem__(inout self, row: Int, col: Int, item: Float32) -> None:
let loc: Int = (row * self.width) + col
self.data.store(loc, item)

fn __getitem__(borrowed self, row: Int, col: Int) -> Float32:
let loc: Int = (row * self.width) + col
return self.data.load(loc)

fn __copyinnit__(inout self, other: Self) -> None:
self.height = other.height
self.width = other.width
self.total_items = other.total_items
self.data = Pointer[Float32].alloc(self.total_items)
for item in range(self.total_items):
self.data.store(item, other.data.load(item))

fn __add__(borrowed self, rhs: Matrix) -> Matrix:
if self.height != rhs.height and self.width != rhs.width:
return Matrix(self.height, self.width, Float32(0))
var new_matirx = Matrix(self.height, self.width, Float32(0))
for row in range(self.height):
for col in range(self.width):
new_matirx[row, col] = self[row, col] + rhs[row, col]
return new_matirx
struct Matrix:
var height: Int
var width: Int
var total_items: Int
var data: Pointer[Float32]

fn __init__(inout self, height: Int, width: Int, default_value: Float32) -> None:
self.height = height
self.width = width
self.total_items = height * width
self.data = Pointer[Float32].alloc(self.total_items)
for item in range(self.total_items):
self.data.store(item, default_value)

fn __setitem__(inout self, row: Int, col: Int, item: Float32) -> None:
let loc: Int = (row * self.width) + col
self.data.store(loc, item)

fn __getitem__(borrowed self, row: Int, col: Int) -> Float32:
let loc: Int = (row * self.width) + col
return self.data.load(loc)

fn __copyinnit__(inout self, other: Self) -> None:
self.height = other.height
self.width = other.width
self.total_items = other.total_items
self.data = Pointer[Float32].alloc(self.total_items)
for item in range(self.total_items):
self.data.store(item, other.data.load(item))

fn __add__(borrowed self, rhs: Matrix) -> Matrix:
if self.height != rhs.height and self.width != rhs.width:
return Matrix(self.height, self.width, Float32(0))
var new_matirx = Matrix(self.height, self.width, Float32(0))
for row in range(self.height):
for col in range(self.width):
new_matirx[row, col] = self[row, col] + rhs[row, col]
return new_matirx
3 replies