/* jquery */ /* jquery accordion style*/ /* jquery init */
Showing posts with label Tutorials. Show all posts
Showing posts with label Tutorials. Show all posts

Python Math - Complex Numbers

Python Coding

Imaginary Numbers and Complex Numbers are vital to solving many mathematical problems - including the study of waves, electric motor efficency and fractals.

Python uses the j symbol to represent the imaginary part of a Complex Numbers.

An example of how this is done is shown in the Python Interactive Mode session below...

>>> a = 4 + 3j
>>> print(a)
(4+3j)

Once created a Complex Number can be treated much like any other number, as below...

>>> a = 1 + 2j
>>> b = 2 + 4j
>>> print('Addition =', a + b)
Addition = (3+6j)
>>> print('Subtraction =', a - b)
Subtraction = (-1-2j)
>>> print('Multiplication =', a * b)
Multiplication = (-6+8j)
>>> print('Division =', a / b)
Division = (2+0j)

And you can extract a Complex Number's real and imaginary components as below...

>>> a = 1 + 2j
>>> b = 3 + 5j
>>> c = a + b
>>> c.real
4.0
>>> c.imag
7.0

More Python Math Coding

Python Math - math module

Python Coding

The Python language math module has a good collection of functionallity. Here are a few highlights to help with solving common math problems.

Constants

There are a number of useful constants in the math module:

#Import math module
import math

math.e
math.pi
math.tau

Factorials

There is a useful factorial() function:

#Import math module
import math

num = input("Enter a number: ")
fac = math.factorial(int(num));
print("Factorial of ", num, " is ", fac)

GCD (Greatest Common Divisor)

This module also has a gcd() function:

#Import math module
import math

print(math.gcd(54, 24))

And there's far more to explore including Logarithmic Functions, Trigonometric Functions, Angular Conversion and Hyperbolic Functions.

For a full list of functionality see the Python3 math module documenation page.

More Python Math Coding

More LEGO® Boost Python Coding

As my original LEGO® Boost kit and Python coding post has received quite a number of page views I thought I'd post some additional information on this topic.

Andrey Pokhilko

I recently came across Andrey Pokhilko and his YouTube video (below). In this video he talks about his own LEGO® Boost Robotics construction and coding project, along with a demonstration of Vernie's capabilities plus a custom power pack hack.

To view Andrey's Python code examples navigate over to his GitHub resources page.

Mikhail Zakharov

I also noticed Mikhail Zakharov has added Python coded Voice Command Control to his own LEGO® Boost Vernie project.

Mikhail's Python source code can be found on his GitLib resources page.

Try My Free Python Coding Tutorials

LEGO® Boost and Python Coding

The LEGO® Boost kit is all about delivering a simplified route into robotics.

You can create models using standard LEGO® bricks. And there's an easy-to-get-started block-based coding app for iOS and Android.

The kit's Move Hub accepts a real-time stream of code instructions, sent via bluetooth, to interact with the built-in motors, LEDs and sensors.

However, it's also possible to interact with the Move Hub using Python, a far more flexible alternative block-based coding.

First you'll need a Python installation on your PC, Mac or Raspberry Pi 3B/3B+/3A+ or Zero W.

You'll also have to install some dependencies:

sudo apt-get install python3-pip
sudo pip3 install pexpect
sudo pip3 install pygatt

Then install the LEGO® Boost Python library module:

wget https://github.com/undera/pylgbst/archive/master.zip
unzip master.zip
cd pylgbst-master
sudo python3 setup.py install

To find out more on using Python with LEGO® Boost (and get to access to plenty of code samples) check out the series of articles by Mike Cook in the official Raspberry Pi magazine, starting at issue 80.

And read my More LEGO® Boost Python Coding post for additional inspiration.

My Free Raspberry Pi Python Coding Tutorials