Skip to content

feat: Implement Array type #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
typedef struct Array
{
double *data;
int *shape;
int ndim;
int size;
} Array;

Array array_new(int ndim, int *shape)
{
int size = 1;
for (int i = 0; i < ndim; i++)
{
size *= shape[i];
}
Array array = {
.data = malloc(size * sizeof(double)),
.ndim = ndim,
.shape = shape,
};
return array;
}

Array array_new_1d(int size)
{
int *shape = malloc(sizeof(int));
shape[0] = size;
return array_new(1, shape);
}

Array array_new_2d(int size_0, int size_1)
{
int *shape = malloc(sizeof(int));
shape[0] = size_0;
shape[1] = size_1;
return array_new(2, shape);
}

Array array_free(Array array)
{
free(array.data);
free(array.shape);
}

Array array_add(Array a, Array b, Array out)
{
if (a.ndim != b.ndim)
printf("error: could not add arrays with different dimensions %d and %d",
a.ndim, b.ndim);
// work in progress
// for (int i = 0; i < 0)
exit(1);
}