Skip to content

Commit 02aa298

Browse files
committed
first commit
0 parents commit 02aa298

File tree

9 files changed

+282
-0
lines changed

9 files changed

+282
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# cmath.gs
2+
This is a math library for complex numbers which is built for [goboscript](https://github.com/aspizu/goboscript).
3+
It is designed to be used with [backpack](https://github.com/aspizu/backpack)
4+
5+
## Installation
6+
To use this, make sure to install [backpack](https://github.com/aspizu/backpack)
7+
8+
You can use the standard library by adding these lines to goboscript.toml:
9+
```toml
10+
[dependencies]
11+
std = "https://github.com/FAReTek1/cmath@<the version you want to use>"
12+
```
13+
14+
Then, add this %include to your gs file:
15+
you can also use this to just %include everything
16+
```rs
17+
%include backpack/cmath/cmath
18+
```

cmath.gs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# dependencies
2+
3+
%include backpack/std/std
4+
5+
################################################################
6+
7+
%include backpack/cmath/cmath/complex
8+
9+
%include backpack/cmath/cmath/operations

cmath/complex.gs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Operations to do with the Complex struct (non-mathematical) and the struct itself
2+
struct Complex {
3+
real,
4+
imag
5+
}
6+
7+
func complex(real, imag) Complex {
8+
# This reduces the amount of 'boilerplate' code for making complex structs.
9+
10+
return Complex{
11+
real: $real,
12+
imag: $imag
13+
};
14+
}
15+
16+
func c_tostr(Complex z) {
17+
if $z.imag > 0 {
18+
return $z.real & " + " & $z.imag & "i";
19+
} else {
20+
return $z.real & " - " & -$z.imag & "i";
21+
}
22+
}

cmath/operations.gs

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
# Mathematical operations with the Complex struct
2+
3+
################################################################
4+
# Basic operations
5+
# Perhaps these should be macros? I don't know...
6+
func c_add(Complex a, Complex b) Complex {
7+
return Complex{
8+
real: $a.real + $b.real,
9+
imag: $a.imag + $b.imag
10+
};
11+
}
12+
13+
func c_sub(Complex a, Complex b) Complex {
14+
return Complex{
15+
real: $a.real - $b.real,
16+
imag: $a.imag - $b.imag
17+
};
18+
}
19+
20+
func c_mul(Complex a, Complex b) Complex {
21+
return Complex{
22+
real: $a.real * $b.real - $a.imag * $b.imag,
23+
imag: $a.real * $b.imag + $a.imag * $b.real
24+
};
25+
}
26+
27+
func c_div(Complex a, Complex b) Complex {
28+
local denom = $b.real * $b.real + $b.imag * $b.imag;
29+
30+
return Complex{
31+
real: ($a.real * $b.real + $a.imag * $b.imag) / denom,
32+
imag: ($a.imag * $b.real - $a.real * $b.imag) / denom
33+
};
34+
}
35+
36+
# No power operator here because it uses exp and ln
37+
38+
################################################################
39+
# Advanced functions
40+
41+
func c_abs(Complex z) {
42+
return sqrt($z.real * $z.real + $z.imag * $z.imag);
43+
}
44+
45+
func c_arg(Complex z) {
46+
return ATAN2($z.imag, $z.real);
47+
}
48+
49+
func c_sgn(complex z) Complex {
50+
# Complex number on the unit circle that is in the direction of the complex number z
51+
return c_div(
52+
$z, Complex{
53+
real: c_abs($z),
54+
imag: 0
55+
}
56+
);
57+
}
58+
59+
func c_exp(Complex z) Complex {
60+
# Same as antiln
61+
return Complex {
62+
real: antiln($z.real) * cos($z.imag * 57.2957795131),
63+
imag: antiln($z.real) * sin($z.imag * 57.2957795131)
64+
};
65+
}
66+
67+
func c_ln(Complex z) Complex {
68+
return Complex {
69+
real: ln(c_abs($z)),
70+
imag: ATAN2($z.imag, $z.real)
71+
};
72+
}
73+
74+
func c_pow(Complex a, Complex b) Complex {
75+
return c_exp(
76+
c_mul($b, c_ln($a))
77+
);
78+
}
79+
80+
func c_conj(Complex z) Complex {
81+
return Complex {
82+
real: $z.real,
83+
imag: -$z.imag
84+
};
85+
}
86+
87+
func c_sqrt(Complex z) Complex {
88+
return c_pow($z, Complex{real: 0.5, imag: 0});
89+
}
90+
91+
################################################################
92+
# Trigonometric functions
93+
94+
func c_sin(Complex z) Complex {
95+
return c_div(c_sub(
96+
c_exp(Complex{real: -$z.imag, imag: $z.real}),
97+
c_exp(Complex{real: $z.imag, imag: -$z.real})
98+
), Complex{real: 0, imag:2});
99+
}
100+
101+
func c_cos(Complex z) Complex {
102+
return c_div(c_add(
103+
c_exp(Complex{real: -$z.imag, imag: $z.real}),
104+
c_exp(Complex{real: $z.imag, imag: -$z.real})
105+
), Complex{real: 2, imag:0});
106+
}
107+
108+
func c_tan(Complex z) Complex {
109+
# Yeah I didn't have this pre-coded so this is a lazy implementation
110+
return c_div(
111+
c_sin($z), c_cos($z)
112+
);
113+
}
114+
115+
################################################################
116+
# Inverse trigonometric functions
117+
118+
func c_asin(Complex z) Complex {
119+
return c_mul(
120+
c_ln(
121+
c_add(
122+
Complex{real:-$z.imag, imag:$z.real},
123+
c_sqrt(
124+
c_sub(
125+
Complex{real:1, imag:0},
126+
c_mul($z, $z)
127+
)
128+
)
129+
)
130+
),
131+
Complex{real:0, imag:-1}
132+
);
133+
}
134+
135+
func c_acos(Complex z) Complex {
136+
local Complex a = c_sqrt(
137+
c_sub(
138+
Complex{real:1, imag:0},
139+
c_mul($z, $z)
140+
)
141+
);
142+
143+
return
144+
c_mul(
145+
c_ln(
146+
c_add(
147+
$z,
148+
Complex{
149+
real:-a.imag, imag:a.real
150+
}
151+
)
152+
),
153+
Complex{real:0, imag:-1}
154+
);
155+
}
156+
157+
func c_atan(Complex z) Complex {
158+
return
159+
c_mul(c_ln(
160+
c_div(
161+
Complex{real: $z.real, imag: $z.imag + 1},
162+
Complex{real: -$z.real, imag: 1-$z.imag}
163+
)
164+
), Complex{real: 0, imag:0.5});
165+
}
166+
167+
################################################################
168+
# Hyperbolic trigonometric functions
169+
func c_sinh(Complex z) Complex {
170+
return Complex{
171+
real: SINH($z.real) * cos($z.imag * 57.2957795131),
172+
imag: COSH($z.real) * sin($z.imag * 57.2957795131)
173+
};
174+
}
175+
176+
func c_cosh(Complex z) Complex {
177+
return Complex{
178+
real: COSH($z.real) * cos($z.imag * 57.2957795131),
179+
imag: -SINH($z.real) * sin($z.imag * 57.2957795131)
180+
};
181+
}
182+
183+
################################################################
184+
# Inverse hyperbolic trigonometric functions
185+
186+
func c_asinh(Complex z) Complex {
187+
# This might need to be improved
188+
return
189+
c_ln(
190+
c_add(
191+
$z,
192+
c_sqrt(
193+
c_add(
194+
c_mul($z, $z),
195+
Complex{real:1, imag:0}
196+
)
197+
)
198+
)
199+
);
200+
}
201+
202+
func c_acosh(Complex z) Complex {
203+
local Complex a = c_mul($z, $z);
204+
205+
return
206+
c_ln(
207+
c_div(
208+
$z,
209+
c_sqrt(Complex{real:-a.real, imag:a.imag})
210+
)
211+
);
212+
}

goboscript.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[dependencies]
2+
std = "https://github.com/FAReTek1/std@v0.0.9"

test/blank.svg

Lines changed: 9 additions & 0 deletions
Loading

test/goboscript.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
no_miscellaneous_limits = false
2+
no_sprite_fencing = false
3+
frame_interpolation = false
4+
high_quality_pen = false

test/main.gs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
costumes "blank.svg";
2+
3+
onflag {
4+
say "Hello, World!";
5+
}

test/stage.gs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
costumes "blank.svg";

0 commit comments

Comments
 (0)