Calling Python from C: A Practical Guide

2025-05-29
Calling Python from C: A Practical Guide

This article provides a practical, step-by-step guide on how to call Python functions from within C code. Starting with the basics, it covers setting up the environment on Linux/Mac, including including the Python.h header, compiling C code, and using Py_Initialize and Py_Finalize to initialize and end the Python interpreter. It details how to load Python modules, get function attributes, call functions (both parameterless and with parameters), and clean up memory. Through concrete code examples, readers learn how to integrate Python functions into C programs, enabling seamless interaction between C and Python code.

Read more
Development cross-language calls

A Programmer's Intro to Integer Partitions and Compositions

2025-05-18
A Programmer's Intro to Integer Partitions and Compositions

LeetArxiv's new series, "What Every Programmer Needs to Know about Enumerative Combinatorics," starts with Chapter 1: an introduction to integer partitions and compositions. This article uses an accessible approach, guiding programmers through these concepts via observation and pattern recognition. C code is provided to generate weak compositions, efficiently enumerating them using binomial coefficients and binary search.

Read more
Development

LeetArxiv: Reimplementing a 1931 Integer Factorization Algorithm

2025-03-30
LeetArxiv: Reimplementing a 1931 Integer Factorization Algorithm

LeetArxiv recreates a 1931 integer factorization algorithm by Lehmer and Powers, based on continued fractions. The article meticulously explains the algorithm, including computing the continued fraction expansion of a square root and using the resulting coefficients (P and A) for factorization. A step-by-step example demonstrates the algorithm's details, successfully factoring 13290059. This algorithm, historically significant, was the first to achieve sub-exponential factoring time.

Read more

LeetArxiv: LeetCode for Arxiv Papers - Hands-on Coding Guides for Research

2025-02-24
LeetArxiv: LeetCode for Arxiv Papers - Hands-on Coding Guides for Research

LeetArxiv offers weekly, step-by-step coding guides based on Arxiv papers, targeting programmers transitioning to research careers. A case study focuses on implementing the AAN Discrete Cosine Transform (DCT) from a 1981 paper. The guide provides a C implementation (easily adaptable to other languages) detailing the DCT-II equation and the AAN optimizations: reducing costly cosine calculations, exploiting symmetry, using precomputed constants, and enabling parallelism. The walkthrough covers the algorithm's flowgraph and code, making complex research accessible through practical coding exercises.

Read more

Emulating a GPU on a CPU Using Finite Field Assembly

2025-01-17
Emulating a GPU on a CPU Using Finite Field Assembly

This article introduces Finite Field Assembly (FF-asm), a novel programming language enabling GPU emulation on CPUs. FF-asm uses a recursive computing paradigm, bypassing the need for SIMD vectorization or OpenMP parallelization. It achieves massive parallel computation on a CPU by creating a custom mathematical system based on finite field theory and congruences. The article provides step-by-step code examples demonstrating addition and multiplication in FF-asm, showcasing its potential for GPU emulation.

Read more