GithubHelp home page GithubHelp logo

complex.lua's Introduction

complex.lua

Version 0.1

complex.lua is a small Lua ligrary that implements complex numbers. It started as an answer on Code Review Stack Exchange.

Usage:

  • complex = require 'complex' — require the library,
  • complex.IMAGINARY_CHAR = 'j' — use electrical notation for serialisation,
  • complex.PREFIX = true — use i/j as a prefix for serialisation,
  • complex.PARENTHESES = true — serialise complex numbers in parentheses,
  • local i = complex.i — set i,
  • complex (re, im) will return the complex number with real part re and imaginary part im,
  • z = complex (re, im); z1 = complex (z) will copy z to z1,
  • z.r and z.i are real and imaginary parts of z, respectively,
  • z.conjugate () — complex conjugation,
  • +, -, * and / operators work as expected; both Lua (real) numbers and complex numbers can be left or right operands: local z = 1 + complex.i, local z = complex.i + 1, local z = complex (1, 0) + complex.i will set z to the same value,
  • unary - will work as expected,
  • == will work as expected,
  • < will give a correct result when both operands are real. In addition, it will return true only if the real parts of operands are equal and the imaginary part of the first one is less than that of the secon one, which can facilitate sorting,
  • complex:abs () — the absolute value,
  • complex:abs2 () — the absolute value squared,
  • complex:arg () — the argument,
  • complex.polar (abs, arg) — return a complex number with the absolute value of abs and argument of arg,
  • complex:exp () — exponent of a complex number,
  • x ^ y will return a complex number, if either Euler's or de Moivre's formula is applicable, i.e., if either the first operand is positive number, or the second argument is real integer,
  • complex:roots (n) will return all n nth roots of a complex number,
  • tostring (x) will serialise the complex number rather prettily (e.g., 5 - 5i, -5i or 5) subject to complex.IMAGINARY_CHAR, complex.PREFIX and complex.PARENTHESES.

Requirements

  • Lua 5.1, 5.2, 5.3, 5.4 or LuaJIT.

Credits

complex.lua is written by Alexander Mashin in 2020 and 2024, originally as a rework of the code written by Nick. Lua is created by Roberto Ierusalimschy.

complex.lua's People

Contributors

alex-mashin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.