Skip to content

Commit 9754d88

Browse files
authored
Add trigonometric functions. (#33)
2 parents 40c4d0c + 03330aa commit 9754d88

File tree

17 files changed

+961
-56
lines changed

17 files changed

+961
-56
lines changed

.github/workflows/benchmark.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ jobs:
4747
run: |
4848
echo "build-output-dir=${{ github.workspace }}/cmake-build" >> "$GITHUB_OUTPUT"
4949
echo "py-build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"
50-
echo "CMAKE_C_COMPILER=${{ matrix.c_compiler }}" >> "$GITHUB_ENV"
51-
echo "CMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}" >> "$GITHUB_ENV"
5250
echo "CMAKE_OPTIONS=${{ matrix.cmake_extra_options }}" >> "$GITHUB_ENV"
5351
5452
- name: Set up Homebrew
@@ -73,7 +71,14 @@ jobs:
7371
- name: Configure CMake
7472
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
7573
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
76-
run: cmake -B "${{github.workspace}}/build" -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DPYTHON_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -DPYTHON_LIBRARY=$(python -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))") -DPYTHON_EXECUTABLE:FILEPATH=`which python` -DSTP_BUILD_COMPONENT_EXECUTABLE=True
74+
run: >
75+
cmake -B "${{github.workspace}}/build"
76+
-DPYTHON_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())")
77+
-DPYTHON_LIBRARY=$(python -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))")
78+
-DPYTHON_EXECUTABLE:FILEPATH=`which python`
79+
-DSTP_BUILD_COMPONENT_EXECUTABLE=True
80+
-DCMAKE_C_COMPILER="$(brew --prefix llvm)/bin/${{ matrix.c_compiler }}"
81+
-DCMAKE_CXX_COMPILER="$(brew --prefix llvm)/bin/${{ matrix.cpp_compiler }}"
7782
shell: bash
7883

7984
- name: CMake Build

.github/workflows/cmake-multi-platform.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ jobs:
2525
#
2626
# To add more build types (Release, Debug, RelWithDebInfo, etc.) customize the build_type list.
2727
matrix:
28-
os: [ubuntu-latest, windows-latest, macos-12, macos-13, macos-14]
28+
os: [ubuntu-latest, windows-2019, macos-12, macos-13, macos-14]
2929
build_type: [Release]
3030
c_compiler: [clang, cl]
3131
python_version: ['3.10', '3.11', '3.12']
3232
include:
33-
- os: windows-latest
33+
- os: windows-2019
3434
c_compiler: cl
3535
cpp_compiler: cl
3636
cmake_extra_options: ''
@@ -54,7 +54,7 @@ jobs:
5454
cpp_compiler: clang++
5555
cmake_extra_options: '-GNinja'
5656
exclude:
57-
- os: windows-latest
57+
- os: windows-2019
5858
c_compiler: clang
5959
- os: ubuntu-latest
6060
c_compiler: cl

.github/workflows/codeql.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,29 @@ jobs:
6262
cache: 'pip'
6363
python-version: '3.12'
6464

65+
- name: Set reusable strings
66+
# Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file.
67+
id: strings
68+
shell: bash
69+
run: |
70+
echo "build-output-dir=${{ github.workspace }}/cmake-build" >> "$GITHUB_OUTPUT"
71+
echo "py-build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"
72+
echo "CMAKE_C_COMPILER=clang" >> "$GITHUB_ENV"
73+
echo "CMAKE_CXX_COMPILER=clang++" >> "$GITHUB_ENV"
74+
75+
- name: Set up Homebrew
76+
id: set-up-homebrew
77+
uses: Homebrew/actions/setup-homebrew@master
78+
79+
- name: Install LLVM
80+
run: |
81+
brew install llvm ninja
82+
echo "PATH=$(brew --prefix llvm)/bin:$PATH" >> "$GITHUB_ENV"
83+
6584
- name: Install dependencies
6685
run: pip install .
6786
shell: bash
87+
6888
# Initializes the CodeQL tools for scanning.
6989
- name: Initialize CodeQL
7090
uses: github/codeql-action/init@v3

.github/workflows/sdist.yml

Lines changed: 0 additions & 38 deletions
This file was deleted.

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ IF(${OPERATING_SYSTEM} MATCHES "Android")
5959
ENDIF()
6060

6161
IF(WIN32)
62-
ADD_COMPILE_DEFINITIONS(WINDOWS)
62+
ADD_COMPILE_DEFINITIONS(WINDOWS NOMINMAX) # Do not add min() and max() macros.
6363
IF(MSVC) # MSVC Has no UTF-8 support by default
6464
ADD_COMPILE_OPTIONS(/utf-8)
6565
ENDIF()
@@ -100,7 +100,7 @@ SET(COMPONENTS
100100
division
101101
root
102102
factorial
103-
)
103+
trig)
104104
# NEW_COMPONENT: PATCH Do NOT remove the previous comment.
105105

106106
SET(TARGETS ${COMPONENTS} util)

include/constants.hpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**************************************************************************************************
2+
* Copyright (c) 2023-2024 NWSOFT *
3+
* *
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy *
5+
* of this software and associated documentation files (the "Software"), to deal *
6+
* in the Software without restriction, including without limitation the rights *
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
8+
* copies of the Software, and to permit persons to whom the Software is *
9+
* furnished to do so, subject to the following conditions: *
10+
* *
11+
* The above copyright notice and this permission notice shall be included in all *
12+
* copies or substantial portions of the Software. *
13+
* *
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
20+
* SOFTWARE. *
21+
**************************************************************************************************/
22+
23+
#pragma once
24+
25+
#include <string_view>
26+
27+
namespace steppable::constants
28+
{
29+
/// @brief 100 digits of pi.
30+
constexpr const std::string_view& PI =
31+
"3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679";
32+
33+
/// @brief Pi multiplied by 2.
34+
// Generated using Python:
35+
// -------------------------------------------------------
36+
// 1 | from decimal import Decimal, getcontext
37+
// 2 | # Set precision to 100 decimal places.
38+
// 3 | getcontext().prec = 100
39+
// 4 | Decimal(
40+
// 5 | 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
41+
// 6 | ) * Decimal(2)
42+
// -------------------------------------------------------
43+
constexpr const std::string_view& TWO_PI = "6.283185307179586231995926937088370323181152343750";
44+
45+
/// @brief Pi divided by 2.
46+
// Generated using Python:
47+
// -------------------------------------------------------
48+
// 1 | from decimal import Decimal, getcontext
49+
// 2 | # Set precision to 100 decimal places.
50+
// 3 | getcontext().prec = 100
51+
// 4 | Decimal(
52+
// 5 | 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
53+
// 6 | ) / Decimal(2)
54+
// -------------------------------------------------------
55+
constexpr const std::string_view& PI_OVER_2 =
56+
"1.570796326794896619231321691639751442098584699687552910487472296153908203143104499314017412835292542";
57+
58+
/// @brief Pi divided by 180 (to convert degrees to radians), correct to 100 decimal places.
59+
// Generated using Python:
60+
// -------------------------------------------------------
61+
// 1 | from decimal import Decimal, getcontext
62+
// 2 | # Set precision to 100 decimal places.
63+
// 3 | getcontext().prec = 100
64+
// 4 | Decimal(
65+
// 5 | 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
66+
// 6 | ) / Decimal(180)
67+
// -------------------------------------------------------
68+
constexpr const std::string_view& PI_OVER_180 =
69+
"0.01745329251994329508887757482524547311994764539930555555555555555555555555555555555555555555555555556";
70+
71+
/// @brief Pi divided by 200 (to convert grads to radians), correct to 100 decimal places.
72+
// Generated using Python:
73+
// -------------------------------------------------------
74+
// 1 | from decimal import Decimal, getcontext
75+
// 2 | # Set precision to 100 decimal places.
76+
// 3 | getcontext().prec = 100
77+
// 4 | Decimal(
78+
// 5 | 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
79+
// 6 | ) / Decimal(200)
80+
// -------------------------------------------------------
81+
constexpr const std::string_view& PI_OVER_200 =
82+
"0.01570796326794896619231321691639716312084074699687552942986246296153903203140449499314017412671058534";
83+
} // namespace steppable::constants

include/fn/basicArithm.hpp

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,154 @@ namespace steppable::__internals::arithmetic
222222
*/
223223
std::string factorial(const std::string& _number, int steps = 2);
224224

225+
/**
226+
* @brief Converts degrees to radians.
227+
*
228+
* @param _deg The angle expressed in degrees.
229+
* @return The equivalent angle in radians.
230+
*/
231+
std::string degToRad(const std::string& _deg);
232+
233+
/**
234+
* @brief Converts gradians to radians.
235+
*
236+
* @param _grad The angle expressed in gradians.
237+
* @return The equivalent angle in radians.
238+
*/
239+
std::string gradToRad(const std::string& _grad);
240+
241+
/**
242+
* @brief Calculates the cosine of a number.
243+
*
244+
* @param x The number to calculate the cosine of.
245+
* @param decimals The number of decimal places to round off to.
246+
* @param mode The mode to calculate the cosine in. 0 = radians (default), 1 = degrees, 2 = gradians.
247+
*
248+
* @return The cosine of the number.
249+
*/
250+
std::string cos(const std::string& x, int decimals, int mode = 0);
251+
252+
/**
253+
* @brief Calculates the sine of a number.
254+
*
255+
* @param x The number to calculate the sine of.
256+
* @param decimals The number of decimal places to round off to.
257+
* @param mode The mode to calculate the sine in. 0 = radians (default), 1 = degrees, 2 = gradians.
258+
*
259+
* @return The sine of the number.
260+
*/
261+
std::string sin(const std::string& x, int decimals, int mode = 0);
262+
263+
/**
264+
* @brief Calculates the tangent of a number.
265+
*
266+
* @param x The number to calculate the tangent of.
267+
* @param decimals The number of decimal places to round off to.
268+
* @param mode The mode to calculate the tangent in. 0 = radians (default), 1 = degrees, 2 = gradians.
269+
*
270+
* @return The tangent of the number.
271+
*/
272+
std::string tan(const std::string& x, int decimals, int mode = 0);
273+
274+
/**
275+
* @brief Calculates the secant of a number.
276+
*
277+
* @param x The number to calculate the secant of.
278+
* @param decimals The number of decimal places to round off to.
279+
* @param mode The mode to calculate the secant in. 0 = radians (default), 1 = degrees, 2 = gradians.
280+
*
281+
* @return The secant of the number.
282+
*/
283+
std::string sec(const std::string& x, int decimals, int mode = 0);
284+
285+
/**
286+
* @brief Calculates the cosecant of a number.
287+
*
288+
* @param x The number to calculate the cosecant of.
289+
* @param decimals The number of decimal places to round off to.
290+
* @param mode The mode to calculate the cosecant in. 0 = radians (default), 1 = degrees, 2 = gradians.
291+
*
292+
* @return The cosecant of the number.
293+
*/
294+
std::string csc(const std::string& x, int decimals, int mode = 0);
295+
296+
/**
297+
* @brief Calculates the cotangent of a number.
298+
*
299+
* @param x The number to calculate the cotangent of.
300+
* @param decimals The number of decimal places to round off to.
301+
* @param mode The mode to calculate the cotangent in. 0 = radians (default), 1 = degrees, 2 = gradians.
302+
*
303+
* @return The cotangent of the number.
304+
*/
305+
std::string cot(const std::string& x, int decimals, int mode = 0);
306+
307+
/**
308+
* @brief Calculates the arc cosine of a number.
309+
*
310+
* @param x The number to calculate the arc cosine of.
311+
* @param decimals The number of decimal places to round off to.
312+
* @param mode The mode to calculate the arc cosine in. 0 = radians (default), 1 = degrees, 2 = gradians.
313+
*
314+
* @return The arc cosine of the number.
315+
*/
316+
std::string acos(const std::string& x, int decimals, int mode = 0);
317+
318+
/**
319+
* @brief Calculates the arc sine of a number.
320+
*
321+
* @param x The number to calculate the arc sine of.
322+
* @param decimals The number of decimal places to round off to.
323+
* @param mode The mode to calculate the arc sine in. 0 = radians (default), 1 = degrees, 2 = gradians.
324+
*
325+
* @return The arc sine of the number.
326+
*/
327+
std::string asin(const std::string& x, int decimals, int mode = 0);
328+
329+
/**
330+
* @brief Calculates the arc tangent of a number.
331+
*
332+
* @param x The number to calculate the arc tangent of.
333+
* @param decimals The number of decimal places to round off to.
334+
* @param mode The mode to calculate the arc tangent in. 0 = radians (default), 1 = degrees, 2 = gradians.
335+
*
336+
* @return The arc tangent of the number.
337+
*/
338+
std::string atan(const std::string& x, int decimals, int mode = 0);
339+
340+
/**
341+
* @brief Calculates the arc secant of a number.
342+
*
343+
* @param x The number to calculate the arc secant of.
344+
* @param decimals The number of decimal places to round off to.
345+
* @param mode The mode to calculate the arc secant in. 0 = radians (default), 1 = degrees, 2 = gradians.
346+
*
347+
* @return The arc secant of the number.
348+
*/
349+
std::string asec(const std::string& x, int decimals, int mode = 0);
350+
351+
/**
352+
* @brief Calculates the arc cosecant of a number.
353+
*
354+
* @param x The number to calculate the arc cosecant of.
355+
* @param decimals The number of decimal places to round off to.
356+
* @param mode The mode to calculate the arc cosecant in. 0 = radians (default), 1 = degrees, 2 = gradians.
357+
*
358+
* @return The arc cosecant of the number.
359+
*/
360+
std::string acsc(const std::string& x, int decimals, int mode = 0);
361+
362+
/**
363+
* @brief Calculates the arc cotangent of a number.
364+
*
365+
* @param x The number to calculate the arc cotangent of.
366+
* @param decimals The number of decimal places to round off to.
367+
* @param mode The mode to calculate the arc cotangent in. 0 = radians (default), 1 = degrees, 2 = gradians.
368+
*
369+
* @return The arc cotangent of the number.
370+
*/
371+
std::string acot(const std::string& x, int decimals, int mode = 0);
372+
225373
/**
226374
* @brief Executes a given predicate function a specified number of times.
227375
*

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ def build_extension(self, ext: CMakeExtension) -> None:
6161
# auxiliary "native" libs
6262

6363
debug = int(os.environ.get("DEBUG", 0)) if self.debug is None else self.debug
64-
cxx_compiler = os.environ.get("CMAKE_CXX_COMPILER", "g++")
65-
c_compiler = os.environ.get("CMAKE_C_COMPILER", "gcc")
64+
cxx_compiler = os.environ.get("CMAKE_CXX_COMPILER", "clang++")
65+
c_compiler = os.environ.get("CMAKE_C_COMPILER", "clang")
6666
cmake_options = os.environ.get("CMAKE_OPTIONS", "")
6767
cfg = "Debug" if debug else "Release"
6868

0 commit comments

Comments
 (0)