M
Modular9mo ago
Harambe

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
2 Replies
zverianskii
zverianskii9mo ago
Are you sure function name copyiNNit is the one that you wanted?
Harambe
Harambe9mo ago
Ah, thanks so much!
Want results from more Discord servers?
Add your server