Thursday, June 7, 2018

Complex numbers - simple calculations

This example is tested with Xcode 9.2 playground in Swift 4.0.3. Basic complex number calculations such as the magnitude are includes as below:


class ComplexNum {
    var real : Float
    var imag : Float
    
    init (real: Float, imag: Float){
        self.real = real
        self.imag = imag
    }
    func show()->String {
        let plusChar = (imag>0) ? "+" : "-"
        return "\(real) \(plusChar) \(abs(imag))i"
    }
    //Magnitude
    func mag()->Float {
        return sqrt(self.real*self.real+self.imag*self.imag)
    }
    //Quick calculation with (i)
    func multiplyPlusI(num:ComplexNum) -> ComplexNum{
        return ComplexNum(real: -num.imag, imag: num.real)
    }
    //Quick calculation with (-i)
    func multiplyMinusI(num:ComplexNum) -> ComplexNum{
        return ComplexNum(real: num.imag, imag: -num.real)
    }
}
func addComplex(a: ComplexNum, b: ComplexNum)->ComplexNum {
    return ComplexNum(real: a.real+b.real, imag: a.imag+b.imag)
}
func multiplyComplex(a: ComplexNum, b: ComplexNum)->ComplexNum {
    return ComplexNum(real: a.real*b.real-a.imag*b.imag, imag: a.real*b.imag+a.imag*b.real)
}

let x = ComplexNum(real: 3, imag: 1)
print("x = \(x.show())")
print("|x| = \(x.mag())")
print("x(j) = \(x.multiplyPlusI(num: x).show())")
print("x(-j) = \(x.multiplyMinusI(num: x).show())")

let y = ComplexNum(real: -1, imag: -2)
print("y = \(y.show())")
print("|y| = \(y.mag())")
print("y(j) = \(x.multiplyPlusI(num: y).show())")
print("y(-j) = \(x.multiplyMinusI(num: y).show())")

print("x + y = \(addComplex(a: x, b: y).show())")
print("x * y = \(multiplyComplex(a: x, b: y).show())")

Results:


x = 3.0 + 1.0i
|x| = 3.16228
x(j) = -1.0 + 3.0i
x(-j) = 1.0 - 3.0i
y = -1.0 - 2.0i
|y| = 2.23607
y(j) = 2.0 - 1.0i
y(-j) = -2.0 + 1.0i
x + y = 2.0 - 1.0i
x * y = -1.0 - 7.0i

Related Information:
Matlab: Real and imaginary parts of complex number and multiplication with imaginary unit j

Friday, April 6, 2018

Mac Technique: How to Enable the Hidden Security & Privacy Option to Allow Apps from Unidentified Developers

With macOS High Sierra 10.13.4, there is no option by default to allow apps from unidentified developers in  -> System Preferences -> Security & Privacy -> General:


Close the window and type the following command in terminal:

sudo spctl --master-disable

and enter your password. Now go back to and the previously hidden Anywhere option is displayed:


Note that if you change a more secure option, the Anywhere option is hidden again when you turn on the Security & Privacy window next time.

Monday, April 2, 2018

Mac Technique: Dr. Eye Chinese-English Bilingual Dictionary with macOS 10.13.4 譯典通英漢雙向字典

Dr. Eye (譯典通), a famous Chinese-English bidirectional dictionary, is now available free with macOS High Sierra 10.13.4.

After installing the latest version of macOS, select Dictionary -> Preferences and 譯典通英漢雙向字典(Traditional Chinese-English).


Now both English and Chinese words may be looked up in the dictionary:



It is interesting that the Bopomofo (ㄅㄆㄇㄈ) Zhuyin Mandarin Phonetic Symbols (注音符號) used in Taiwan are displayed in this dictionary.

The Dr. Eye dictionary is also available with iOS 11.3.

Friday, March 9, 2018

C language with Mac: Hello World with Mac's Terminal

This post shows how to execute a hello world program in C with the following steps:

1. In terminal, create a C file called hello.c:

nano hello.c

2. Edit the hello.c file as:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

3. In terminal, execute the file with:

cc hello.c
./a.out

Result:


References

How to run C program on Mac OS X using Terminal? (StackOverflow)
How to print Hello World in C on Mac (YouTube)
C++ with Mac: Hello World with Mac's Terminal
Matlab: Run Hello World files in C/C++ with MEX command

Thursday, March 1, 2018

C++ with Mac: Install Boost Libraries

To install the Boost C++ libraries on a Mac

1. Download the latest Boost package, e.g. the boost_1_66_0.tar.bz2 file.

2. In terminal, move the the directory with the downloaded file and type:

tar --bzip2 -xf boost_1_66_0.tar.bz2

3. There are three types of Boost libraries:

(1) Header-only libraries. Just include these files without compilation. Most files belong to this type.
(2) Libraries required to be built. e.g. Boost.System, Boost.Thread, and Boost.Timer.
(3) Optional separately-compiled binaries, e.g. Boost Math, and Boost Random.

4. Follow the instructions in section 5.1 "Easy Build and Install" of Boost's getting started guide for unix-variants.

5. Follow the instructions in section 6 "Link Your Program to a Boost Library" of Boost's getting started guide for unix-variants. The main way A to link libraries may be like this command:

c++ -I ~your_path/boost_1_66_0/ test.cpp -o test ~your_path/boost_1_66_0/stage/lib/libboost_regex.a

Follow instructions in section 6.2. The result should be like this:



References

Boost libraries (boost.org)
Install boost on Mac OSX
Getting Started on Unix VariantsGetting Started on Unix Variants

Friday, February 23, 2018

C++ with Mac: Hello World with Mac's Terminal

This post shows how to execute a C++ hello world program with the following steps:

1. In terminal, create a C++ file called hello.cpp:

nano hello.cpp

2. Edit the hello.cpp file as:

#include <iostream>

using namespace std;

int main() {
    cout << "Hello, World!\n";
    return 0;
}

3. In terminal, execute the file with:

g++ hello.cpp
./a.out

Result:


References

Compiling simple Hello World program on OS X via command line (StackOverflow)
C language with Mac: Hello World with Mac's Terminal
Matlab: Run Hello World files in C/C++ with MEX command

Saturday, February 10, 2018