Urgent.News

What's breaking now, across thousands of outlets.

Tech

Writing a C matrix library

I'm toying with the idea to write a neural network library in C. Which is quite an undertaking. To approach it in baby steps, I want to build some smaller building blocks first. Setting up a foundation of sorts. The random number library is one of them. Because neural networks need random weight initialisation. But another important one could be: Matrix math (mostly multiplication). You don't…

The author is working on creating a neural network library in C and has decided to start with building smaller components, such as a random number library and matrix math. Matrix multiplication is highlighted as an important aspect of neural networks due to its ability to vectorize the network.

To represent a matrix in C, the author opts for using structs, as they are the primary method for grouping data together. The Mat struct is designed to store the dimensions (rows and columns) and a pointer to the actual floating point data inside the matrix.

The author discusses memory management, emphasizing their preference for not including memory management within the matrix library. Instead, they choose to manage memory allocation externally, passing existing memory to the library for manipulation. This approach makes the matrix library solely responsible for matrix-related functionality without concerning itself with memory management.

For printing a matrix, the author creates a mat_print() function that iterates through each element and displays it formatted with six decimal places. Once a matrix is created, the author proceeds to implement matrix multiplication using the mat_mul() function.

The author prefers passing Mat structs by value to the mat_mul() function, which simplifies the process since the Mat struct only holds size information and a pointer to the data. By doing this, the function can directly access the data without needing to pass pointers, making it more straightforward and efficient.

The implementation of matrix multiplication involves calculating the dot product of rows from matrix a and columns from matrix b. The resulting values are stored in matrix c, with the author looping over the dimensions of c to compute each element.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

How I turn Shopify pre-orders into a daily bake sheet

If you run a small bakery on Shopify and take orders for a pickup day, you probably know this evening routine. Export the orders, open a spreadsheet, filter by date, then add up how many loaves, boxes…

  • Shopify pre-orders create daily baking challenges due to inconsistent date formats.
  • Tikkivo BakeList app consolidates quantities by product and variant, flags inactive orders.
  • Free plan accommodates up to 30 orders monthly, paid plans start at $19 per month.

More from Sunday 1 November →