Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the at sign (@) used in the c source code for numpy?

I've been perusing some of the source code for numpy and I noticed that a lot of the c source code uses the construction @variablename@. For example, in the file "npy_math_complex.c.src" (located here):

/*==========================================================
* Constants
*=========================================================*/
static const @ctype@ c_1@c@ = {1.0@C@, 0.0};
static const @ctype@ c_half@c@ = {0.5@C@, 0.0};
static const @ctype@ c_i@c@ = {0.0, 1.0@C@};
static const @ctype@ c_ihalf@c@ = {0.0, 0.5@C@};

What do @ctype@ and @c@ mean? Are these some sort of macros? I'm guessing they aren't normal C-macros since I've looked at the relevant header files listed in the file and they don't appear to define any macros using "@".

Is @name@ some sort of macro used by distutils when compiling c code into a python module?

I've never seen an @ sign used in c code before and so I'm a bit confused...

like image 861
FormerPhysicist Avatar asked Aug 24 '17 23:08

FormerPhysicist


People also ask

What is NumPy and how to learn it?

In this tutorial, we are going to discuss some problems and the solution with NumPy practical examples and code. As you might know, NumPy is one of the important Python modules used in the field of data science and machine learning. As a beginner, it is very important to know about a few NumPy practical examples.

What is the use of NumPy search?

Searching is a technique that helps finds the place of a given element or value in the list. In Numpy, one can perform various searching operations using the various functions that are provided in the library like argmax, argmin, etc. This function returns indices of the maximum element of the array in a particular axis.

What is Numerical Python (NumPy)?

NumPy is a Python library used for working with arrays. It also has functions for working in domain of linear algebra, fourier transform, and matrices. NumPy was created in 2005 by Travis Oliphant. It is an open source project and you can use it freely. NumPy stands for Numerical Python.

What does the @ operator mean in NumPy?

BRAVO! If you are referring to some code in a python notebook which is using Numpy library, then @ operator means Matrix Multiplication. For example: import numpy as np def forward (xi, W1, b1, W2, b2): z1 = W1 @ xi + b1 a1 = sigma (z1) z2 = W2 @ a1 + b2 return z2, a1


1 Answers

It's because these files are templates. If I remember correctly NumPy uses several template engines (thanks @user2357112 for helping me finding the appropriate one):

  • tempita (for Cython files)
  • another one for C files
  • another one for FORTRAN files

and the second one is actually responsible to convert these to "regular" C files - before these get compiled.

Basically these functions will be cloned a lot of times and for each of the functions a special placeholder is inserted between the %.

For example in this case it begins with:

/**begin repeat
 * #type = npy_float, npy_double, npy_longdouble#
 * #ctype = npy_cfloat,npy_cdouble,npy_clongdouble#
 * #c = f, , l#
 * #C = F, , L#
 * ....
 */

So in the first iteration @ctype@ will be replaced with npy_cfloat and @c@ with f and @C@ with F:

static const npy_cfloat c_1f = {1.0F, 0.0};
static const npy_cfloat c_halff = {0.5F, 0.0};
static const npy_cfloat c_if = {0.0, 1.0F};
static const npy_cfloat c_ihalff = {0.0, 0.5F};

In the next iteration is @ctype@ npy_cdouble, ...

static const npy_cdouble c_1 = {1.0, 0.0};
static const npy_cdouble c_half = {0.5, 0.0};
static const npy_cdouble c_i = {0.0, 1.0};
static const npy_cdouble c_ihalf = {0.0, 0.5};

and in the third iteration:

static const npy_clongdouble c_1l = {1.0L, 0.0};
static const npy_clongdouble c_halfl = {0.5L, 0.0};
static const npy_clongdouble c_il = {0.0, 1.0L};
static const npy_clongdouble c_ihalfl = {0.0, 0.5L};

these are then compiled as normal C files.

like image 192
MSeifert Avatar answered Oct 02 '22 22:10

MSeifert