Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trigonometric functions. #33

Merged
merged 16 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ jobs:
run: |
echo "build-output-dir=${{ github.workspace }}/cmake-build" >> "$GITHUB_OUTPUT"
echo "py-build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"
echo "CMAKE_C_COMPILER=${{ matrix.c_compiler }}" >> "$GITHUB_ENV"
echo "CMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}" >> "$GITHUB_ENV"
echo "CMAKE_OPTIONS=${{ matrix.cmake_extra_options }}" >> "$GITHUB_ENV"

- name: Set up Homebrew
Expand All @@ -73,7 +71,14 @@ jobs:
- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
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
run: >
cmake -B "${{github.workspace}}/build"
-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
-DCMAKE_C_COMPILER="$(brew --prefix llvm)/bin/${{ matrix.c_compiler }}"
-DCMAKE_CXX_COMPILER="$(brew --prefix llvm)/bin/${{ matrix.cpp_compiler }}"
shell: bash

- name: CMake Build
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ jobs:
#
# To add more build types (Release, Debug, RelWithDebInfo, etc.) customize the build_type list.
matrix:
os: [ubuntu-latest, windows-latest, macos-12, macos-13, macos-14]
os: [ubuntu-latest, windows-2019, macos-12, macos-13, macos-14]
build_type: [Release]
c_compiler: [clang, cl]
python_version: ['3.10', '3.11', '3.12']
include:
- os: windows-latest
- os: windows-2019
c_compiler: cl
cpp_compiler: cl
cmake_extra_options: ''
Expand All @@ -54,7 +54,7 @@ jobs:
cpp_compiler: clang++
cmake_extra_options: '-GNinja'
exclude:
- os: windows-latest
- os: windows-2019
c_compiler: clang
- os: ubuntu-latest
c_compiler: cl
Expand Down
20 changes: 20 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,29 @@ jobs:
cache: 'pip'
python-version: '3.12'

- name: Set reusable strings
# Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file.
id: strings
shell: bash
run: |
echo "build-output-dir=${{ github.workspace }}/cmake-build" >> "$GITHUB_OUTPUT"
echo "py-build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"
echo "CMAKE_C_COMPILER=clang" >> "$GITHUB_ENV"
echo "CMAKE_CXX_COMPILER=clang++" >> "$GITHUB_ENV"

- name: Set up Homebrew
id: set-up-homebrew
uses: Homebrew/actions/setup-homebrew@master

- name: Install LLVM
run: |
brew install llvm ninja
echo "PATH=$(brew --prefix llvm)/bin:$PATH" >> "$GITHUB_ENV"

- name: Install dependencies
run: pip install .
shell: bash

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
Expand Down
38 changes: 0 additions & 38 deletions .github/workflows/sdist.yml

This file was deleted.

4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ IF(${OPERATING_SYSTEM} MATCHES "Android")
ENDIF()

IF(WIN32)
ADD_COMPILE_DEFINITIONS(WINDOWS)
ADD_COMPILE_DEFINITIONS(WINDOWS NOMINMAX) # Do not add min() and max() macros.
IF(MSVC) # MSVC Has no UTF-8 support by default
ADD_COMPILE_OPTIONS(/utf-8)
ENDIF()
Expand Down Expand Up @@ -100,7 +100,7 @@ SET(COMPONENTS
division
root
factorial
)
trig)
# NEW_COMPONENT: PATCH Do NOT remove the previous comment.

SET(TARGETS ${COMPONENTS} util)
Expand Down
83 changes: 83 additions & 0 deletions include/constants.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**************************************************************************************************
* Copyright (c) 2023-2024 NWSOFT *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy *
* of this software and associated documentation files (the "Software"), to deal *
* in the Software without restriction, including without limitation the rights *
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in all *
* copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
* SOFTWARE. *
**************************************************************************************************/

#pragma once

#include <string_view>

namespace steppable::constants
{
/// @brief 100 digits of pi.
constexpr const std::string_view& PI =
"3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679";

/// @brief Pi multiplied by 2.
// Generated using Python:
// -------------------------------------------------------
// 1 | from decimal import Decimal, getcontext
// 2 | # Set precision to 100 decimal places.
// 3 | getcontext().prec = 100
// 4 | Decimal(
// 5 | 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
// 6 | ) * Decimal(2)
// -------------------------------------------------------
constexpr const std::string_view& TWO_PI = "6.283185307179586231995926937088370323181152343750";

/// @brief Pi divided by 2.
// Generated using Python:
// -------------------------------------------------------
// 1 | from decimal import Decimal, getcontext
// 2 | # Set precision to 100 decimal places.
// 3 | getcontext().prec = 100
// 4 | Decimal(
// 5 | 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
// 6 | ) / Decimal(2)
// -------------------------------------------------------
constexpr const std::string_view& PI_OVER_2 =
"1.570796326794896619231321691639751442098584699687552910487472296153908203143104499314017412835292542";

/// @brief Pi divided by 180 (to convert degrees to radians), correct to 100 decimal places.
// Generated using Python:
// -------------------------------------------------------
// 1 | from decimal import Decimal, getcontext
// 2 | # Set precision to 100 decimal places.
// 3 | getcontext().prec = 100
// 4 | Decimal(
// 5 | 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
// 6 | ) / Decimal(180)
// -------------------------------------------------------
constexpr const std::string_view& PI_OVER_180 =
"0.01745329251994329508887757482524547311994764539930555555555555555555555555555555555555555555555555556";

/// @brief Pi divided by 200 (to convert grads to radians), correct to 100 decimal places.
// Generated using Python:
// -------------------------------------------------------
// 1 | from decimal import Decimal, getcontext
// 2 | # Set precision to 100 decimal places.
// 3 | getcontext().prec = 100
// 4 | Decimal(
// 5 | 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
// 6 | ) / Decimal(200)
// -------------------------------------------------------
constexpr const std::string_view& PI_OVER_200 =
"0.01570796326794896619231321691639716312084074699687552942986246296153903203140449499314017412671058534";
} // namespace steppable::constants
148 changes: 148 additions & 0 deletions include/fn/basicArithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,154 @@ namespace steppable::__internals::arithmetic
*/
std::string factorial(const std::string& _number, int steps = 2);

/**
* @brief Converts degrees to radians.
*
* @param _deg The angle expressed in degrees.
* @return The equivalent angle in radians.
*/
std::string degToRad(const std::string& _deg);

/**
* @brief Converts gradians to radians.
*
* @param _grad The angle expressed in gradians.
* @return The equivalent angle in radians.
*/
std::string gradToRad(const std::string& _grad);

/**
* @brief Calculates the cosine of a number.
*
* @param x The number to calculate the cosine of.
* @param decimals The number of decimal places to round off to.
* @param mode The mode to calculate the cosine in. 0 = radians (default), 1 = degrees, 2 = gradians.
*
* @return The cosine of the number.
*/
std::string cos(const std::string& x, int decimals, int mode = 0);

/**
* @brief Calculates the sine of a number.
*
* @param x The number to calculate the sine of.
* @param decimals The number of decimal places to round off to.
* @param mode The mode to calculate the sine in. 0 = radians (default), 1 = degrees, 2 = gradians.
*
* @return The sine of the number.
*/
std::string sin(const std::string& x, int decimals, int mode = 0);

/**
* @brief Calculates the tangent of a number.
*
* @param x The number to calculate the tangent of.
* @param decimals The number of decimal places to round off to.
* @param mode The mode to calculate the tangent in. 0 = radians (default), 1 = degrees, 2 = gradians.
*
* @return The tangent of the number.
*/
std::string tan(const std::string& x, int decimals, int mode = 0);

/**
* @brief Calculates the secant of a number.
*
* @param x The number to calculate the secant of.
* @param decimals The number of decimal places to round off to.
* @param mode The mode to calculate the secant in. 0 = radians (default), 1 = degrees, 2 = gradians.
*
* @return The secant of the number.
*/
std::string sec(const std::string& x, int decimals, int mode = 0);

/**
* @brief Calculates the cosecant of a number.
*
* @param x The number to calculate the cosecant of.
* @param decimals The number of decimal places to round off to.
* @param mode The mode to calculate the cosecant in. 0 = radians (default), 1 = degrees, 2 = gradians.
*
* @return The cosecant of the number.
*/
std::string csc(const std::string& x, int decimals, int mode = 0);

/**
* @brief Calculates the cotangent of a number.
*
* @param x The number to calculate the cotangent of.
* @param decimals The number of decimal places to round off to.
* @param mode The mode to calculate the cotangent in. 0 = radians (default), 1 = degrees, 2 = gradians.
*
* @return The cotangent of the number.
*/
std::string cot(const std::string& x, int decimals, int mode = 0);

/**
* @brief Calculates the arc cosine of a number.
*
* @param x The number to calculate the arc cosine of.
* @param decimals The number of decimal places to round off to.
* @param mode The mode to calculate the arc cosine in. 0 = radians (default), 1 = degrees, 2 = gradians.
*
* @return The arc cosine of the number.
*/
std::string acos(const std::string& x, int decimals, int mode = 0);

/**
* @brief Calculates the arc sine of a number.
*
* @param x The number to calculate the arc sine of.
* @param decimals The number of decimal places to round off to.
* @param mode The mode to calculate the arc sine in. 0 = radians (default), 1 = degrees, 2 = gradians.
*
* @return The arc sine of the number.
*/
std::string asin(const std::string& x, int decimals, int mode = 0);

/**
* @brief Calculates the arc tangent of a number.
*
* @param x The number to calculate the arc tangent of.
* @param decimals The number of decimal places to round off to.
* @param mode The mode to calculate the arc tangent in. 0 = radians (default), 1 = degrees, 2 = gradians.
*
* @return The arc tangent of the number.
*/
std::string atan(const std::string& x, int decimals, int mode = 0);

/**
* @brief Calculates the arc secant of a number.
*
* @param x The number to calculate the arc secant of.
* @param decimals The number of decimal places to round off to.
* @param mode The mode to calculate the arc secant in. 0 = radians (default), 1 = degrees, 2 = gradians.
*
* @return The arc secant of the number.
*/
std::string asec(const std::string& x, int decimals, int mode = 0);

/**
* @brief Calculates the arc cosecant of a number.
*
* @param x The number to calculate the arc cosecant of.
* @param decimals The number of decimal places to round off to.
* @param mode The mode to calculate the arc cosecant in. 0 = radians (default), 1 = degrees, 2 = gradians.
*
* @return The arc cosecant of the number.
*/
std::string acsc(const std::string& x, int decimals, int mode = 0);

/**
* @brief Calculates the arc cotangent of a number.
*
* @param x The number to calculate the arc cotangent of.
* @param decimals The number of decimal places to round off to.
* @param mode The mode to calculate the arc cotangent in. 0 = radians (default), 1 = degrees, 2 = gradians.
*
* @return The arc cotangent of the number.
*/
std::string acot(const std::string& x, int decimals, int mode = 0);

/**
* @brief Executes a given predicate function a specified number of times.
*
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def build_extension(self, ext: CMakeExtension) -> None:
# auxiliary "native" libs

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

Expand Down
Loading
Loading