Skip to content

Commit 30ab0b8

Browse files
authored
Fix sign error and add basic graphing utilities (#60)
This PR fixes #59, and adds the following features - `Parameter` class support. Enables calling functions with named arguments. - Preliminary graph support - Localized messages in `mat2d` It fixes - Segmentation fault in `add` and `subtract`. The following refactorings are used - Test files are grouped by component <details> <summary> **Changelog** </summary> - **Add graphing functions** - **Add legends** - **Allow interpolation on graphs.** - Using cubic interpolation and linear interpolation to allow graphing with fewer sample points. - **[BREAKING] Fix `constexpr` errors.** - [BREAKING] Switched `ConsoleOutput::write` to using `std::string_view`. `std::string` should be automatically passed as `std::string_view`. - **Fixed more constexpr errors** - **Add `Parameter` support** - **Bug fixes to conPlot.** - **Fix `std::any_cast` errors.** - **Add better detection for Windows** Signed-off-by: Andy Zhang <z-c-ge@outlook.com> - **Added documentation and code refactoring** - Move out conPlot and parameter.hpp definitions to .cpp files - Fix CMakeLists.txt to avoid duplicate builds - Add documentation to conPlot and parameter.hpp - **Fix build errors** - **Refactoring: Test files are grouped by component** - **Add support for bar plots** - **Add shading support for simple curves** - **[#59] Fix addition.** - **Fix segmentation fault in subtract.** - **Fix result issue for adding negative numbers** - Fix wrong results for input like -1, 1 - **Add translations for `mat2d`** </details>
2 parents 2dc4254 + 8b371b2 commit 30ab0b8

Some content is hidden

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

75 files changed

+2757
-870
lines changed

.clang-tidy

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ Checks: ['bugprone-*',
1212
'readability-*',
1313
'zircon-*',
1414
# Remove the following:
15-
'-bugprone-easily-swappable-parameters',
16-
'-bugprone-suspicious-semicolon',
1715
'-cert-dcl37-c',
1816
'-cert-dcl50-cpp',
1917
'-cert-dcl51-cpp',
20-
'-cppcoreguidelines-avoid-magic-numbers',
18+
'-cppcoreguidelines-avoid-do-while',
2119
'-cppcoreguidelines-pro-bounds-array-to-pointer-decay',
2220
'-cppcoreguidelines-pro-type-vararg',
2321
'-modernize-use-trailing-return-type',

.clangd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ Diagnostics:
2828
'cert-dcl51-cpp',
2929
'cppcoreguidelines-pro-type-vararg',
3030
'readability-braces-around-statements',
31-
'readability-identifier-length'
31+
'readability-identifier-length',
32+
'cppcoreguidelines-avoid-do-while'
3233
]
3334

3435
---

.idea/runConfigurations/Run_Tests.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CMakeLists.txt

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,24 +111,11 @@ SET(COMPONENTS
111111
calc::root
112112
calc::subtract
113113
calc::trig
114-
matrix::ref)
114+
matrix::ref
115+
)
115116
# NEW_COMPONENT: PATCH Do NOT remove the previous comment.
116117

117118
SET(TARGETS ${COMPONENTS} util)
118-
SET(TEST_TARGETS_TEMP util fraction number mat2d factors format ${COMPONENTS})
119-
120-
FOREACH(TEST_TARGET IN LISTS TEST_TARGETS_TEMP)
121-
SET(TARGET_NAME "test")
122-
STRING(REPLACE "::" ";" COMPONENT_PARTS ${TEST_TARGET})
123-
LIST(LENGTH COMPONENT_PARTS LEN)
124-
IF(LEN EQUAL 2)
125-
LIST(GET COMPONENT_PARTS 1 TEST_TARGET)
126-
ENDIF()
127-
128-
CAPITALIZE(${TEST_TARGET} FILE_NAME)
129-
STRING(CONCAT TARGET_NAME ${TARGET_NAME} ${FILE_NAME})
130-
LIST(APPEND TEST_TARGETS ${TARGET_NAME})
131-
ENDFOREACH()
132119

133120
ADD_SUBDIRECTORY(src/)
134121
ADD_SUBDIRECTORY(lib/)

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@
55
[![CMake Build](https://github.com/ZCG-coder/Steppable/actions/workflows/cmake-multi-platform.yml/badge.svg)](https://github.com/ZCG-coder/Steppable/actions/workflows/cmake-multi-platform.yml)
66
[![Doxygen Deploy](https://github.com/ZCG-coder/Steppable/actions/workflows/doxygen-gh-pages.yml/badge.svg)](https://github.com/ZCG-coder/Steppable/actions/workflows/doxygen-gh-pages.yml)
77

8-
This project aims to make a Computer Algebra System (CAS) from scratch, and without any external libraries.
8+
This project aims to build a Computer Algebra System (CAS) from scratch, without any external libraries.
9+
10+
All documentation, usage instructions, and developer guides are available in the [project Wiki](https://github.com/ZCG-coder/Steppable/wiki).
11+
12+
---
13+
For contribution guidelines, see [CONTRIBUTING.md](CONTRIBUTING.md).

include/colors.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
#pragma once
3939

40+
#include <functional>
4041
#include <ostream>
4142

4243
/**
@@ -61,6 +62,8 @@
6162

6263
namespace steppable::__internals::utils
6364
{
65+
using ColorFunc = std::function<std::ostream&(std::ostream&)>;
66+
6467
/**
6568
* @brief Check if the output stream is a terminal.
6669
*
@@ -87,6 +90,14 @@ namespace steppable::__internals::utils
8790
*/
8891
namespace colors
8992
{
93+
/**
94+
* @brief Does nothing.
95+
*
96+
* @param[in] stream The output stream to modify.
97+
* @return The modified output stream.
98+
*/
99+
std::ostream& keepOriginal(std::ostream& stream);
100+
90101
/**
91102
* @brief Set the text color to black.
92103
*

include/conPlot/conPlot.hpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**************************************************************************************************
2+
* Copyright (c) 2023-2025 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 "conPlot/conPlotTypes.hpp"
26+
27+
/**
28+
* @namespace steppable::graphing
29+
* @brief Graphing utilities for showing graphs in the console.
30+
*/
31+
namespace steppable::graphing
32+
{
33+
void conPlot(const std::vector<GraphFn>& f,
34+
const GraphOptions& graphOptions,
35+
const std::vector<LineOptions>& linesOptions);
36+
37+
void conPlotBar(const std::vector<std::vector<Number>>& numbers,
38+
const BarGraphOptions& graphOptions,
39+
const std::vector<BarOptions>& barsOptions);
40+
} // namespace steppable::graphing

include/conPlot/conPlotInternals.hpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**************************************************************************************************
2+
* Copyright (c) 2023-2025 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 "conPlot/conPlotTypes.hpp"
26+
#include "steppable/number.hpp"
27+
28+
#include <map>
29+
30+
/**
31+
* @namespace steppable::graphing::__internals
32+
* @brief Graphing utilities for showing graphs in the console.
33+
*/
34+
namespace steppable::graphing::__internals
35+
{
36+
void conPlotLine(const Number& xGridSize,
37+
const Number& yGridSize,
38+
const Number& yMax,
39+
const GraphOptions* graphOptions,
40+
const LineOptions* lineOptions,
41+
prettyPrint::ConsoleOutput* canvas,
42+
std::map<Number, Number>& fnValues);
43+
44+
void drawTicks(prettyPrint::ConsoleOutput* canvas,
45+
const Number& xGridSize,
46+
const Number& yGridSize,
47+
const Number& yMax,
48+
const GraphOptionsBase* graphOptions);
49+
50+
void drawGrid(prettyPrint::ConsoleOutput* canvas, const GraphOptionsBase* graphOptions);
51+
} // namespace steppable::graphing::__internals
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**************************************************************************************************
2+
* Copyright (c) 2023-2025 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 <map>
26+
27+
namespace steppable::graphing
28+
{
29+
/**
30+
* @brief Fill in integral values with linear interpolation.
31+
*
32+
* @param data An x-y corresponding table of values. Must have at least 2 items.
33+
* @param xMin Minimum `x` value.
34+
* @param xMax Maximum `x` value.
35+
*/
36+
void linearInterpolateFill(std::map<long long, long long>* data, long long xMin, long long xMax);
37+
38+
/**
39+
* @brief Fill in integral values with cubic interpolation.
40+
*
41+
* @param data An x-y corresponding table of values. Must have at least 4 items.
42+
* @param xMin Minimum `x` value.
43+
* @param xMax Maximum `x` value.
44+
*/
45+
void cubicInterpolateFill(std::map<long long, long long>* data, long long xMin, long long xMax);
46+
} // namespace steppable::graphing

0 commit comments

Comments
 (0)