Skip to content

Commit 900857a

Browse files
author
MarkBaker
committed
Initial commit of a new library that provides procedural use of functionality from my Complex numbers library; separated out because I just got so fe up with the constant moans and complaints of people that object to the fact that PHP can't autoload functions/files, which meant that it required a files entry in the autoload section of the composer file.
So I get now have to maintain a third branch in the main Complex library, plus this additional repository... but as long as the entitled ones are happy, the reduction in my lready minimal free time, and the state of my mental health, are a small price to pay (as far as they are concerned)
0 parents  commit 900857a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+4687
-0
lines changed

.gitattributes

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#
2+
# Exclude these files from release archives.
3+
# This will also make them unavailable when using Composer with `--prefer-dist`.
4+
# If you develop using Composer, use `--prefer-source`.
5+
# https://www.reddit.com/r/PHP/comments/2jzp6k/i_dont_need_your_tests_in_my_production
6+
# https://blog.madewithlove.be/post/gitattributes/
7+
#
8+
/unitTests export-ignore
9+
.gitattributes export-ignore
10+
.gitignore export-ignore
11+
.phpcs.xml export-ignore
12+
.phpcs.xml.dist export-ignore
13+
phpcs.xml.dist export-ignore
14+
phpunit.xml.dist export-ignore
15+
16+
#
17+
# Auto detect text files and perform LF normalization
18+
# http://davidlaing.com/2012/09/19/customise-your-gitattributes-to-become-a-git-ninja/
19+
#
20+
* text=auto
21+
22+
#
23+
# The above will handle all files NOT found below
24+
#
25+
*.md text
26+
*.php text

.github/workflows/main.yml

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
name: main
2+
on: [ push, pull_request ]
3+
jobs:
4+
test:
5+
runs-on: ubuntu-latest
6+
strategy:
7+
matrix:
8+
experimental:
9+
- false
10+
php-version:
11+
- '7.2'
12+
- '7.3'
13+
- '7.4'
14+
- '8.0'
15+
16+
include:
17+
- php-version: '8.1'
18+
experimental: true
19+
20+
name: PHP ${{ matrix.php-version }}
21+
22+
steps:
23+
- name: Checkout
24+
uses: actions/checkout@v2
25+
26+
- name: Setup PHP, with composer and extensions
27+
uses: shivammathur/setup-php@v2
28+
with:
29+
php-version: ${{ matrix.php-version }}
30+
coverage: none
31+
32+
- name: Get composer cache directory
33+
id: composer-cache
34+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
35+
36+
- name: Cache composer dependencies
37+
uses: actions/cache@v2
38+
with:
39+
path: ${{ steps.composer-cache.outputs.dir }}
40+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
41+
restore-keys: ${{ runner.os }}-composer-
42+
43+
- name: Delete composer lock file
44+
id: composer-lock
45+
if: ${{ matrix.php-version == '8.1' }}
46+
run: |
47+
echo "::set-output name=flags::--ignore-platform-reqs"
48+
49+
- name: Install dependencies
50+
run: composer update --no-progress --prefer-dist --optimize-autoloader ${{ steps.composer-lock.outputs.flags }}
51+
52+
- name: Setup problem matchers for PHP
53+
run: echo "::add-matcher::${{ runner.tool_cache }}/php.json"
54+
55+
- name: Setup problem matchers for PHPUnit
56+
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
57+
58+
- name: "Run PHPUnit tests (Experimental: ${{ matrix.experimental }})"
59+
env:
60+
FAILURE_ACTION: "${{ matrix.experimental == true }}"
61+
run: vendor/bin/phpunit --verbose || $FAILURE_ACTION
62+
63+
phpcs:
64+
runs-on: ubuntu-latest
65+
steps:
66+
- name: Checkout
67+
uses: actions/checkout@v2
68+
69+
- name: Setup PHP, with composer and extensions
70+
uses: shivammathur/setup-php@v2
71+
with:
72+
php-version: 7.4
73+
coverage: none
74+
tools: cs2pr
75+
76+
- name: Get composer cache directory
77+
id: composer-cache
78+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
79+
80+
- name: Cache composer dependencies
81+
uses: actions/cache@v2
82+
with:
83+
path: ${{ steps.composer-cache.outputs.dir }}
84+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
85+
restore-keys: ${{ runner.os }}-composer-
86+
87+
- name: Install dependencies
88+
run: composer install --no-progress --prefer-dist --optimize-autoloader
89+
90+
- name: Code style with PHP_CodeSniffer
91+
run: ./vendor/bin/phpcs -q --report=checkstyle classes/src/ | cs2pr
92+
93+
versions:
94+
runs-on: ubuntu-latest
95+
steps:
96+
- name: Checkout
97+
uses: actions/checkout@v2
98+
99+
- name: Setup PHP, with composer and extensions
100+
uses: shivammathur/setup-php@v2
101+
with:
102+
php-version: 7.4
103+
coverage: none
104+
tools: cs2pr
105+
106+
- name: Get composer cache directory
107+
id: composer-cache
108+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
109+
110+
- name: Cache composer dependencies
111+
uses: actions/cache@v2
112+
with:
113+
path: ${{ steps.composer-cache.outputs.dir }}
114+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
115+
restore-keys: ${{ runner.os }}-composer-
116+
117+
- name: Install dependencies
118+
run: composer install --no-progress --prefer-dist --optimize-autoloader
119+
120+
- name: Code Version Compatibility check with PHP_CodeSniffer
121+
run: ./vendor/bin/phpcs -q --report-width=200 --report=summary,full classes/src/ --standard=PHPCompatibility --runtime-set testVersion 7.2-
122+
123+
coverage:
124+
runs-on: ubuntu-latest
125+
steps:
126+
- name: Checkout
127+
uses: actions/checkout@v2
128+
129+
- name: Setup PHP, with composer and extensions
130+
uses: shivammathur/setup-php@v2
131+
with:
132+
php-version: 7.4
133+
coverage: pcov
134+
135+
- name: Get composer cache directory
136+
id: composer-cache
137+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
138+
139+
- name: Cache composer dependencies
140+
uses: actions/cache@v2
141+
with:
142+
path: ${{ steps.composer-cache.outputs.dir }}
143+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
144+
restore-keys: ${{ runner.os }}-composer-
145+
146+
- name: Install dependencies
147+
run: composer install --no-progress --prefer-dist --optimize-autoloader
148+
149+
- name: Test Coverage
150+
run: ./vendor/bin/phpunit --verbose --coverage-text

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Complex.phar
2+
unitTests/codeCoverage
3+
analysis
4+
5+
## IDE support
6+
*.buildpath
7+
*.project
8+
/.settings
9+
/.idea
10+
11+
## Don't commit personal PHPCS rulesets.
12+
phpcs.xml
13+
.phpcs.xml
14+
15+
## Don't commit the vendor directory nor the lock file (as this is a library).
16+
/vendor/
17+
composer.lock

README.md

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
PHPComplexFunctions
2+
=====================
3+
4+
---
5+
6+
PHP Function Library for working with Complex numbers
7+
8+
[![Build Status](https://github.com/MarkBaker/PHPComplexFunctions/workflows/main/badge.svg)](https://github.com/MarkBaker/PHPComplexFunctions/actions)
9+
[![Total Downloads](https://img.shields.io/packagist/dt/markbaker/complex-functions)](https://packagist.org/packages/markbaker/complex-functions)
10+
[![Latest Stable Version](https://img.shields.io/github/v/release/MarkBaker/PHPComplexFunctions)](https://packagist.org/packages/markbaker/complex-functions)
11+
[![License](https://img.shields.io/github/license/MarkBaker/PHPComplexFunctions)](https://packagist.org/packages/markbaker/complex-functions)
12+
13+
[![Complex Numbers](https://imgs.xkcd.com/comics/complex_numbers_2x.png)](https://xkcd.com/2028/)
14+
15+
---
16+
17+
The library currently provides the following operations:
18+
19+
- addition
20+
- subtraction
21+
- multiplication
22+
- division
23+
- division by
24+
- division into
25+
26+
together with functions for
27+
28+
- theta (polar theta angle)
29+
- rho (polar distance/radius)
30+
- conjugate
31+
* negative
32+
- inverse (1 / complex)
33+
- cos (cosine)
34+
- acos (inverse cosine)
35+
- cosh (hyperbolic cosine)
36+
- acosh (inverse hyperbolic cosine)
37+
- sin (sine)
38+
- asin (inverse sine)
39+
- sinh (hyperbolic sine)
40+
- asinh (inverse hyperbolic sine)
41+
- sec (secant)
42+
- asec (inverse secant)
43+
- sech (hyperbolic secant)
44+
- asech (inverse hyperbolic secant)
45+
- csc (cosecant)
46+
- acsc (inverse cosecant)
47+
- csch (hyperbolic secant)
48+
- acsch (inverse hyperbolic secant)
49+
- tan (tangent)
50+
- atan (inverse tangent)
51+
- tanh (hyperbolic tangent)
52+
- atanh (inverse hyperbolic tangent)
53+
- cot (cotangent)
54+
- acot (inverse cotangent)
55+
- coth (hyperbolic cotangent)
56+
- acoth (inverse hyperbolic cotangent)
57+
- sqrt (square root)
58+
- exp (exponential)
59+
- ln (natural log)
60+
- log10 (base-10 log)
61+
- log2 (base-2 log)
62+
- pow (raised to the power of a real number)
63+
64+
65+
---
66+
67+
# Usage
68+
69+
To create a new complex object, you can provide either the real, imaginary and suffix parts as individual values, or as an array of values passed passed to the constructor; or a string representing the value. e.g
70+
71+
```php
72+
$real = 1.23;
73+
$imaginary = -4.56;
74+
$suffix = 'i';
75+
76+
$complexObject = new Complex\Complex($real, $imaginary, $suffix);
77+
```
78+
or
79+
```php
80+
$real = 1.23;
81+
$imaginary = -4.56;
82+
$suffix = 'i';
83+
84+
$arguments = [$real, $imaginary, $suffix];
85+
86+
$complexObject = new Complex\Complex($arguments);
87+
```
88+
or
89+
```php
90+
$complexString = '1.23-4.56i';
91+
92+
$complexObject = new Complex\Complex($complexString);
93+
```
94+
95+
Complex objects are immutable: whenever you call a method or pass a complex value to a function that returns a complex value, a new Complex object will be returned, and the original will remain unchanged.
96+
This also allows you to chain multiple methods as you would for a fluent interface (as long as they are methods that will return a Complex result).
97+
98+
## Performing Mathematical Operations
99+
100+
To perform mathematical operations with Complex values, you can call the appropriate method against a complex value, passing other values as arguments
101+
102+
```php
103+
$complexString1 = '1.23-4.56i';
104+
$complexString2 = '2.34+5.67i';
105+
106+
$complexObject = new Complex\Complex($complexString1);
107+
echo $complexObject->add($complexString2);
108+
```
109+
110+
or use the static Operation methods
111+
```php
112+
$complexString1 = '1.23-4.56i';
113+
$complexString2 = '2.34+5.67i';
114+
115+
echo Complex\Operations::add($complexString1, $complexString2);
116+
```
117+
118+
or procedurally, you can pass all values to the appropriate (namespaced) function
119+
```php
120+
$complexString1 = '1.23-4.56i';
121+
$complexString2 = '2.34+5.67i';
122+
123+
echo Complex\add($complexString1, $complexString2);
124+
```
125+
If you want to perform the same operation against multiple values (e.g. to add three or more complex numbers), then you can pass multiple arguments to any of the operations.
126+
127+
You can pass these arguments as Complex objects, or as an array or string that will parse to a complex object.
128+
129+
## Using functions
130+
131+
When calling any of the available functions for a complex value, you can either call the relevant method for the Complex object
132+
```php
133+
$complexString = '1.23-4.56i';
134+
135+
$complexObject = new Complex\Complex($complexString);
136+
echo $complexObject->sinh();
137+
```
138+
139+
or use the static Functions methods
140+
```php
141+
$complexString = '1.23-4.56i';
142+
143+
echo Complex\Functions::sinh($complexString);
144+
```
145+
146+
or you can call the function as you would in procedural code, passing the Complex object as an argument
147+
```php
148+
$complexString = '1.23-4.56i';
149+
150+
$complexObject = new Complex\Complex($complexString);
151+
echo Complex\sinh($complexObject);
152+
```
153+
154+
When called procedurally using the function, you can pass in the argument as a Complex object, or as an array or string that will parse to a complex object.
155+
```php
156+
$complexString = '1.23-4.56i';
157+
158+
echo Complex\sinh($complexString);
159+
```
160+
161+
In the case of the `pow()` function (the only implemented function that requires an additional argument) you need to pass both arguments when calling the function procedurally
162+
163+
```php
164+
$complexString = '1.23-4.56i';
165+
166+
$complexObject = new Complex\Complex($complexString);
167+
echo Complex\pow($complexObject, 2);
168+
```
169+
or pass the additional argument when calling the method
170+
```php
171+
$complexString = '1.23-4.56i';
172+
173+
$complexObject = new Complex\Complex($complexString);
174+
echo $complexObject->pow(2);
175+
```

0 commit comments

Comments
 (0)