mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
commit
7ae9717f26
23
.github/workflows/ccpp.yml
vendored
Normal file
23
.github/workflows/ccpp.yml
vendored
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
name: C/C++ CI
|
||||||
|
|
||||||
|
on: [push]
|
||||||
|
# push:
|
||||||
|
# branches: [ master ]
|
||||||
|
# pull_request:
|
||||||
|
# branches: [ master ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os: [ubuntu-latest, windows-latest, macOS-latest]
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@master
|
||||||
|
with:
|
||||||
|
submodules: true
|
||||||
|
- name: configure
|
||||||
|
run: cmake -B ./build -S .
|
||||||
|
- name: build
|
||||||
|
run: cmake --build build
|
3
.github/workflows/cpplint_modified_files.yml
vendored
3
.github/workflows/cpplint_modified_files.yml
vendored
@ -42,7 +42,8 @@ jobs:
|
|||||||
print("g++:")
|
print("g++:")
|
||||||
# compile_exts = tuple(".c .c++ .cc .cpp .cu .cxx".split())
|
# compile_exts = tuple(".c .c++ .cc .cpp .cu .cxx".split())
|
||||||
# compile_files = [file for file in cpp_files if file.lower().endswith(compile_exts)]
|
# compile_files = [file for file in cpp_files if file.lower().endswith(compile_exts)]
|
||||||
subprocess.run(["g++"] + cpp_files, check=True, text=True)
|
for cpp_file in cpp_files:
|
||||||
|
subprocess.run(["g++", cpp_file], check=True, text=True)
|
||||||
|
|
||||||
upper_files = [file for file in cpp_files if file != file.lower()]
|
upper_files = [file for file in cpp_files if file != file.lower()]
|
||||||
if upper_files:
|
if upper_files:
|
||||||
|
35
.github/workflows/gh-pages.yml
vendored
Normal file
35
.github/workflows/gh-pages.yml
vendored
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
name: Doxygen CI
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [master]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@master
|
||||||
|
with:
|
||||||
|
submodules: true
|
||||||
|
- name: Install requirements
|
||||||
|
run: |
|
||||||
|
sudo apt -qq -y update
|
||||||
|
sudo apt -qq install doxygen graphviz ninja-build
|
||||||
|
- name: configure
|
||||||
|
run: cmake -G Ninja -B ./build -S .
|
||||||
|
- name: build
|
||||||
|
run: cmake --build build -t doc
|
||||||
|
- name: gh-pages
|
||||||
|
uses: actions/checkout@master
|
||||||
|
with:
|
||||||
|
ref: "gh-pages"
|
||||||
|
clean: false
|
||||||
|
- name: Move & Commit files
|
||||||
|
run: |
|
||||||
|
cp -rp ./build/html/* . && rm -rf ./build && ls -lah
|
||||||
|
git config --global user.name github-actions
|
||||||
|
git config --global user.email '${GITHUB_ACTOR}@users.noreply.github.com'
|
||||||
|
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
|
||||||
|
git add *
|
||||||
|
git commit -m "Documentation for $GITHUB_SHA" || true
|
||||||
|
git push --force || true
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -33,3 +33,5 @@
|
|||||||
a.out
|
a.out
|
||||||
*.out
|
*.out
|
||||||
*.app
|
*.app
|
||||||
|
|
||||||
|
build/
|
66
CMakeLists.txt
Normal file
66
CMakeLists.txt
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.3)
|
||||||
|
project(Algorithms_in_C++
|
||||||
|
LANGUAGES CXX
|
||||||
|
VERSION 1.0.0
|
||||||
|
DESCRIPTION "Set of algorithms implemented in C++."
|
||||||
|
)
|
||||||
|
|
||||||
|
# set(CMAKE_CXX_CPPLINT "~/anaconda3/bin/cpplint --filter=-legal/copyright --std=c++11")
|
||||||
|
# find_program(CLANG_FORMAT "clang-format")
|
||||||
|
|
||||||
|
option(USE_OPENMP "flag to use OpenMP for multithreading" ON)
|
||||||
|
|
||||||
|
cmake_policy(SET CMP0054 NEW)
|
||||||
|
cmake_policy(SET CMP0057 NEW)
|
||||||
|
find_package(Doxygen OPTIONAL_COMPONENTS dot dia)
|
||||||
|
if(DOXYGEN_FOUND)
|
||||||
|
set(DOXYGEN_GENERATE_MAN NO)
|
||||||
|
set(DOXYGEN_USE_MATHJAX YES)
|
||||||
|
set(DOXYGEN_GENERATE_HTML YES)
|
||||||
|
set(DOXYGEN_INLINE_SOURCES YES)
|
||||||
|
set(DOXYGEN_CREATE_SUBDIRS YES)
|
||||||
|
set(DOCYGEN_GENERATE_TREEVIEW YES)
|
||||||
|
set(DOXYGEN_STRIP_CODE_COMMENTS NO)
|
||||||
|
set(DOXYGEN_BUILTIN_STL_SUPPORT YES)
|
||||||
|
if(MSVC)
|
||||||
|
set(DOXYGEN_CPP_CLI_SUPPORT YES)
|
||||||
|
endif()
|
||||||
|
set(DOXYGEN_MATHJAX_RELPATH "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML")
|
||||||
|
if(Doxygen_dot_FOUND)
|
||||||
|
set(DOXYGEN_HAVE_DOT YES)
|
||||||
|
set(DOXYGEN_CALL_GRAPH YES)
|
||||||
|
set(DOXYGEN_INTERACTIVE_SVG YES)
|
||||||
|
set(DOXYGEN_DOT_IMAGE_FORMAT "svg")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
doxygen_add_docs(
|
||||||
|
doc
|
||||||
|
${PROJECT_SOURCE_DIR}
|
||||||
|
COMMENT "Generate documentation"
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 11)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
|
if(MSVC)
|
||||||
|
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
|
||||||
|
add_compile_options(/Za)
|
||||||
|
endif(MSVC)
|
||||||
|
|
||||||
|
add_subdirectory(math)
|
||||||
|
add_subdirectory(others)
|
||||||
|
add_subdirectory(computer_oriented_statistical_methods)
|
||||||
|
|
||||||
|
if(USE_OPENMP)
|
||||||
|
find_package(OpenMP)
|
||||||
|
if (OpenMP_C_FOUND)
|
||||||
|
message(STATUS "Building with OpenMP Multithreading.")
|
||||||
|
else()
|
||||||
|
message(STATUS "No OpenMP found, no multithreading.")
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
|
||||||
|
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
|
||||||
|
include(CPack)
|
38
DIRECTORY.md
38
DIRECTORY.md
@ -10,13 +10,13 @@
|
|||||||
* [Sudoku Solve](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/sudoku_solve.cpp)
|
* [Sudoku Solve](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/sudoku_solve.cpp)
|
||||||
|
|
||||||
## Computer Oriented Statistical Methods
|
## Computer Oriented Statistical Methods
|
||||||
* [Bisection Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Bisection_method.CPP)
|
* [Bisection Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/bisection_method.cpp)
|
||||||
* [False-Position](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/false-position.cpp)
|
* [False Position](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/false_position.cpp)
|
||||||
* [Gaussian Elimination](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Gaussian_elimination.cpp)
|
* [Gaussian Elimination](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/gaussian_elimination.cpp)
|
||||||
* [Newton Raphson](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Newton_Raphson.CPP)
|
* [Newton Raphson Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/newton_raphson_method.cpp)
|
||||||
* [Ordinary Least Squares Regressor](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/ordinary_least_squares_regressor.cpp)
|
* [Ordinary Least Squares Regressor](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/ordinary_least_squares_regressor.cpp)
|
||||||
* [Secant Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Secant_method.CPP)
|
* [Secant Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/secant_method.cpp)
|
||||||
* [Successive Approximation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/successive_approximation.CPP)
|
* [Successive Approximation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/successive_approximation.cpp)
|
||||||
|
|
||||||
## Data Structure
|
## Data Structure
|
||||||
* [Avltree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structure/AVLtree.cpp)
|
* [Avltree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structure/AVLtree.cpp)
|
||||||
@ -116,7 +116,7 @@
|
|||||||
* [Power For Huge Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/power_for_huge_numbers.cpp)
|
* [Power For Huge Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/power_for_huge_numbers.cpp)
|
||||||
* [Prime Factorization](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/prime_factorization.cpp)
|
* [Prime Factorization](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/prime_factorization.cpp)
|
||||||
* [Prime Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/prime_numbers.cpp)
|
* [Prime Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/prime_numbers.cpp)
|
||||||
* [Primes Up To 10^8](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/primes_up_to_10^8.cpp)
|
* [Primes Up To Billion](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/primes_up_to_billion.cpp)
|
||||||
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/sieve_of_eratosthenes.cpp)
|
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/sieve_of_eratosthenes.cpp)
|
||||||
* [Sqrt Double](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/sqrt_double.cpp)
|
* [Sqrt Double](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/sqrt_double.cpp)
|
||||||
|
|
||||||
@ -132,28 +132,27 @@
|
|||||||
* [Union Of 2 Arrays](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/Union_of_2_arrays.cpp)
|
* [Union Of 2 Arrays](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/Union_of_2_arrays.cpp)
|
||||||
|
|
||||||
## Others
|
## Others
|
||||||
* [Buzz Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Buzz_number.cpp)
|
* [Buzz Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/buzz_number.cpp)
|
||||||
* [Decimal To Binary](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Decimal%20To%20Binary.cpp)
|
* [Decimal To Binary](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/decimal_to_binary.cpp)
|
||||||
* [Decimal To Hexadecimal ](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Decimal%20To%20Hexadecimal%20.cpp)
|
* [Decimal To Hexadecimal](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/decimal_to_hexadecimal.cpp)
|
||||||
* [Decimal To Roman Numeral](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Decimal%20to%20Roman%20Numeral.cpp)
|
* [Decimal To Roman Numeral](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/decimal_to_roman_numeral.cpp)
|
||||||
* [Fast Interger Input](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/fast_interger_input.cpp)
|
* [Fast Interger Input](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/fast_interger_input.cpp)
|
||||||
* [Fibonacci](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/fibonacci.cpp)
|
* [Fibonacci Fast](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/fibonacci_fast.cpp)
|
||||||
* [Gcd Of N Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/GCD_of_n_numbers.cpp)
|
* [Gcd Of N Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/gcd_of_n_numbers.cpp)
|
||||||
* [Happy Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/happy_number.cpp)
|
* [Happy Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/happy_number.cpp)
|
||||||
* [Matrix Exponentiation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/matrix_exponentiation.cpp)
|
* [Matrix Exponentiation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/matrix_exponentiation.cpp)
|
||||||
* [Measure Time Elapsed](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/measure_time_elapsed.cpp)
|
* [Measure Time Elapsed](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/measure_time_elapsed.cpp)
|
||||||
* [Palindromeofnumber](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Palindromeofnumber.cpp)
|
* [Palindromeofnumber](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Palindromeofnumber.cpp)
|
||||||
* [Paranthesis Matching](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Paranthesis%20Matching.cpp)
|
* [Paranthesis Matching](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/paranthesis_matching.cpp)
|
||||||
* [Pascal Triangle](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/pascal_triangle.cpp)
|
* [Pascal Triangle](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/pascal_triangle.cpp)
|
||||||
* [Primality Test](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Primality%20Test.cpp)
|
* [Primality Test](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/primality_test.cpp)
|
||||||
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/sieve_of_Eratosthenes.cpp)
|
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/sieve_of_Eratosthenes.cpp)
|
||||||
* [Smallest-Circle](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/smallest-circle.cpp)
|
* [Smallest-Circle](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/smallest-circle.cpp)
|
||||||
* [Sparse Matrix](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Sparse%20matrix.cpp)
|
* [Sparse Matrix](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/sparse_matrix.cpp)
|
||||||
* [Spiral Print](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/spiral_print.cpp)
|
* [Spiral Print](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/spiral_print.cpp)
|
||||||
* [Stairs Pattern](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/stairs_pattern.cpp)
|
* [Stairs Pattern](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/stairs_pattern.cpp)
|
||||||
* [Strassen Matrix Multiplication](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Strassen%20Matrix%20Multiplication.cpp)
|
* [String Fibonacci](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/string_fibonacci.cpp)
|
||||||
* [String Fibonacci](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/String%20Fibonacci.cpp)
|
* [Tower Of Hanoi](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/tower_of_hanoi.cpp)
|
||||||
* [Tower Of Hanoi](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Tower%20of%20Hanoi.cpp)
|
|
||||||
* [Vector Important Functions](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/vector_important_functions.cpp)
|
* [Vector Important Functions](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/vector_important_functions.cpp)
|
||||||
|
|
||||||
## Probability
|
## Probability
|
||||||
@ -200,6 +199,7 @@
|
|||||||
* [Radix Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Radix%20Sort.cpp)
|
* [Radix Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Radix%20Sort.cpp)
|
||||||
* [Selection Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Selection%20Sort.cpp)
|
* [Selection Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Selection%20Sort.cpp)
|
||||||
* [Shell Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Shell%20Sort.cpp)
|
* [Shell Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Shell%20Sort.cpp)
|
||||||
|
* [Shell Sort2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/shell_sort2.cpp)
|
||||||
* [Slow Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Slow%20Sort.cpp)
|
* [Slow Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Slow%20Sort.cpp)
|
||||||
* [Swap Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/swap_sort.cpp)
|
* [Swap Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/swap_sort.cpp)
|
||||||
* [Tim Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Tim%20Sort.cpp)
|
* [Tim Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Tim%20Sort.cpp)
|
||||||
|
@ -1,53 +0,0 @@
|
|||||||
#include <iostream.h>
|
|
||||||
#include <conio.h>
|
|
||||||
#include <math.h>
|
|
||||||
|
|
||||||
float eq(float i)
|
|
||||||
{
|
|
||||||
return (pow(i, 3) - (4 * i) - 9); // original equation
|
|
||||||
}
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
float a, b, x, z;
|
|
||||||
clrscr();
|
|
||||||
for (int i = 0; i < 100; i++)
|
|
||||||
{
|
|
||||||
z = eq(i);
|
|
||||||
if (z >= 0)
|
|
||||||
{
|
|
||||||
b = i;
|
|
||||||
a = --i;
|
|
||||||
goto START;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
START:
|
|
||||||
|
|
||||||
cout << "\nFirst initial: " << a;
|
|
||||||
cout << "\nSecond initial: " << b;
|
|
||||||
for (i = 0; i < 100; i++)
|
|
||||||
{
|
|
||||||
x = (a + b) / 2;
|
|
||||||
z = eq(x);
|
|
||||||
cout << "\n\nz: " << z << "\t[" << a << " , " << b << " | Bisect: " << x << "]";
|
|
||||||
|
|
||||||
if (z < 0)
|
|
||||||
{
|
|
||||||
a = x;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
b = x;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (z > 0 && z < 0.0009) // stoping criteria
|
|
||||||
{
|
|
||||||
goto END;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
END:
|
|
||||||
cout << "\n\nRoot: " << x;
|
|
||||||
getch();
|
|
||||||
}
|
|
18
computer_oriented_statistical_methods/CMakeLists.txt
Normal file
18
computer_oriented_statistical_methods/CMakeLists.txt
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
|
||||||
|
# with full pathname. RELATIVE may makes it easier to extract an executable name
|
||||||
|
# automatically.
|
||||||
|
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp )
|
||||||
|
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
|
||||||
|
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
|
||||||
|
foreach( testsourcefile ${APP_SOURCES} )
|
||||||
|
# I used a simple string replace, to cut off .cpp.
|
||||||
|
string( REPLACE ".cpp" "" testname ${testsourcefile} )
|
||||||
|
add_executable( ${testname} ${testsourcefile} )
|
||||||
|
|
||||||
|
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX)
|
||||||
|
if(OpenMP_CXX_FOUND)
|
||||||
|
target_link_libraries(${testname} OpenMP::OpenMP_CXX)
|
||||||
|
endif()
|
||||||
|
install(TARGETS ${testname} DESTINATION "bin/stats")
|
||||||
|
|
||||||
|
endforeach( testsourcefile ${APP_SOURCES} )
|
@ -1,63 +0,0 @@
|
|||||||
#include <iostream>
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
int mat_size, i, j, step;
|
|
||||||
|
|
||||||
cout << "Matrix size: ";
|
|
||||||
cin >> mat_size;
|
|
||||||
|
|
||||||
double mat[mat_size + 1][mat_size + 1], x[mat_size][mat_size + 1];
|
|
||||||
|
|
||||||
cout << endl
|
|
||||||
<< "Enter value of the matrix: " << endl;
|
|
||||||
for (i = 0; i < mat_size; i++)
|
|
||||||
{
|
|
||||||
for (j = 0; j <= mat_size; j++)
|
|
||||||
{
|
|
||||||
cin >> mat[i][j]; //Enter (mat_size*mat_size) value of the matrix.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (step = 0; step < mat_size - 1; step++)
|
|
||||||
{
|
|
||||||
for (i = step; i < mat_size - 1; i++)
|
|
||||||
{
|
|
||||||
double a = (mat[i + 1][step] / mat[step][step]);
|
|
||||||
|
|
||||||
for (j = step; j <= mat_size; j++)
|
|
||||||
mat[i + 1][j] = mat[i + 1][j] - (a * mat[step][j]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cout << endl
|
|
||||||
<< "Matrix using Gaussian Elimination method: " << endl;
|
|
||||||
for (i = 0; i < mat_size; i++)
|
|
||||||
{
|
|
||||||
for (j = 0; j <= mat_size; j++)
|
|
||||||
{
|
|
||||||
x[i][j] = mat[i][j];
|
|
||||||
cout << mat[i][j] << " ";
|
|
||||||
}
|
|
||||||
cout << endl;
|
|
||||||
}
|
|
||||||
cout << endl
|
|
||||||
<< "Value of the Gaussian Elimination method: " << endl;
|
|
||||||
for (i = mat_size - 1; i >= 0; i--)
|
|
||||||
{
|
|
||||||
double sum = 0;
|
|
||||||
for (j = mat_size - 1; j > i; j--)
|
|
||||||
{
|
|
||||||
x[i][j] = x[j][j] * x[i][j];
|
|
||||||
sum = x[i][j] + sum;
|
|
||||||
}
|
|
||||||
if (x[i][i] == 0)
|
|
||||||
x[i][i] = 0;
|
|
||||||
else
|
|
||||||
x[i][i] = (x[i][mat_size] - sum) / (x[i][i]);
|
|
||||||
|
|
||||||
cout << "x" << i << "= " << x[i][i] << endl;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
@ -1,53 +0,0 @@
|
|||||||
#include <iostream.h>
|
|
||||||
#include <conio.h>
|
|
||||||
#include <math.h>
|
|
||||||
|
|
||||||
float eq(float i)
|
|
||||||
{
|
|
||||||
return (pow(i, 3) - (4 * i) - 9); // original equation
|
|
||||||
}
|
|
||||||
float eq_der(float i)
|
|
||||||
{
|
|
||||||
return ((3 * pow(i, 2)) - 4); // derivative of equation
|
|
||||||
}
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
float a, b, z, c, m, n;
|
|
||||||
clrscr();
|
|
||||||
for (int i = 0; i < 100; i++)
|
|
||||||
{
|
|
||||||
z = eq(i);
|
|
||||||
if (z >= 0)
|
|
||||||
{
|
|
||||||
b = i;
|
|
||||||
a = --i;
|
|
||||||
goto START;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
START:
|
|
||||||
|
|
||||||
cout << "\nFirst initial: " << a;
|
|
||||||
cout << "\nSecond initial: " << b;
|
|
||||||
c = (a + b) / 2;
|
|
||||||
|
|
||||||
for (i = 0; i < 100; i++)
|
|
||||||
{
|
|
||||||
float h;
|
|
||||||
m = eq(c);
|
|
||||||
n = eq_der(c);
|
|
||||||
|
|
||||||
z = c - (m / n);
|
|
||||||
c = z;
|
|
||||||
|
|
||||||
if (m > 0 && m < 0.009) // stoping criteria
|
|
||||||
{
|
|
||||||
goto END;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
END:
|
|
||||||
cout << "\n\nRoot: " << z;
|
|
||||||
getch();
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
#include <iostream.h>
|
|
||||||
#include <conio.h>
|
|
||||||
#include <math.h>
|
|
||||||
|
|
||||||
float eq(float i)
|
|
||||||
{
|
|
||||||
return (pow(i, 3) - (4 * i) - 9); // original equation
|
|
||||||
}
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
float a, b, z, c, m, n;
|
|
||||||
clrscr();
|
|
||||||
for (int i = 0; i < 100; i++)
|
|
||||||
{
|
|
||||||
z = eq(i);
|
|
||||||
if (z >= 0)
|
|
||||||
{
|
|
||||||
b = i;
|
|
||||||
a = --i;
|
|
||||||
goto START;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
START:
|
|
||||||
|
|
||||||
cout << "\nFirst initial: " << a;
|
|
||||||
cout << "\nSecond initial: " << b;
|
|
||||||
for (i = 0; i < 100; i++)
|
|
||||||
{
|
|
||||||
float h, d;
|
|
||||||
m = eq(a);
|
|
||||||
n = eq(b);
|
|
||||||
|
|
||||||
c = ((a * n) - (b * m)) / (n - m);
|
|
||||||
a = b;
|
|
||||||
b = c;
|
|
||||||
|
|
||||||
z = eq(c);
|
|
||||||
if (z > 0 && z < 0.09) // stoping criteria
|
|
||||||
{
|
|
||||||
goto END;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
END:
|
|
||||||
cout << "\n\nRoot: " << c;
|
|
||||||
getch();
|
|
||||||
}
|
|
40
computer_oriented_statistical_methods/bisection_method.cpp
Normal file
40
computer_oriented_statistical_methods/bisection_method.cpp
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#include <cmath>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
static float eq(float i) {
|
||||||
|
return (std::pow(i, 3) - (4 * i) - 9); // original equation
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
float a, b, x, z;
|
||||||
|
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
z = eq(i);
|
||||||
|
if (z >= 0) {
|
||||||
|
b = i;
|
||||||
|
a = --i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "\nFirst initial: " << a;
|
||||||
|
std::cout << "\nSecond initial: " << b;
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
x = (a + b) / 2;
|
||||||
|
z = eq(x);
|
||||||
|
std::cout << "\n\nz: " << z << "\t[" << a << " , " << b
|
||||||
|
<< " | Bisect: " << x << "]";
|
||||||
|
|
||||||
|
if (z < 0) {
|
||||||
|
a = x;
|
||||||
|
} else {
|
||||||
|
b = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (z > 0 && z < 0.0009) // stoping criteria
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "\n\nRoot: " << x;
|
||||||
|
return 0;
|
||||||
|
}
|
@ -1,9 +1,11 @@
|
|||||||
#include<stdlib.h>
|
#include <cmath>
|
||||||
#include <math.h>
|
#include <cstdlib>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
float eq(float i) {
|
|
||||||
|
static float eq(float i) {
|
||||||
return (pow(i, 3) - (4 * i) - 9); // origial equation
|
return (pow(i, 3) - (4 * i) - 9); // origial equation
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
float a, b, z, c, m, n;
|
float a, b, z, c, m, n;
|
||||||
system("clear");
|
system("clear");
|
||||||
@ -12,12 +14,13 @@ int main() {
|
|||||||
if (z >= 0) {
|
if (z >= 0) {
|
||||||
b = i;
|
b = i;
|
||||||
a = --i;
|
a = --i;
|
||||||
goto START;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
START:
|
|
||||||
std::cout << "\nFirst initial: " << a;
|
std::cout << "\nFirst initial: " << a;
|
||||||
std::cout << "\nSecond initial: " << b;
|
std::cout << "\nSecond initial: " << b;
|
||||||
|
|
||||||
for (int i = 0; i < 100; i++) {
|
for (int i = 0; i < 100; i++) {
|
||||||
float h, d;
|
float h, d;
|
||||||
m = eq(a);
|
m = eq(a);
|
||||||
@ -26,10 +29,10 @@ int main() {
|
|||||||
a = c;
|
a = c;
|
||||||
z = eq(c);
|
z = eq(c);
|
||||||
if (z > 0 && z < 0.09) { // stoping criteria
|
if (z > 0 && z < 0.09) { // stoping criteria
|
||||||
goto END;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
END:
|
|
||||||
std::cout << "\n\nRoot: " << c;
|
std::cout << "\n\nRoot: " << c;
|
||||||
system("pause");
|
return 0;
|
||||||
}
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int mat_size, i, j, step;
|
||||||
|
|
||||||
|
std::cout << "Matrix size: ";
|
||||||
|
std::cin >> mat_size;
|
||||||
|
|
||||||
|
double mat[mat_size + 1][mat_size + 1], x[mat_size][mat_size + 1];
|
||||||
|
|
||||||
|
std::cout << std::endl << "Enter value of the matrix: " << std::endl;
|
||||||
|
for (i = 0; i < mat_size; i++) {
|
||||||
|
for (j = 0; j <= mat_size; j++) {
|
||||||
|
std::cin >>
|
||||||
|
mat[i][j]; // Enter (mat_size*mat_size) value of the matrix.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (step = 0; step < mat_size - 1; step++) {
|
||||||
|
for (i = step; i < mat_size - 1; i++) {
|
||||||
|
double a = (mat[i + 1][step] / mat[step][step]);
|
||||||
|
|
||||||
|
for (j = step; j <= mat_size; j++)
|
||||||
|
mat[i + 1][j] = mat[i + 1][j] - (a * mat[step][j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << std::endl
|
||||||
|
<< "Matrix using Gaussian Elimination method: " << std::endl;
|
||||||
|
for (i = 0; i < mat_size; i++) {
|
||||||
|
for (j = 0; j <= mat_size; j++) {
|
||||||
|
x[i][j] = mat[i][j];
|
||||||
|
std::cout << mat[i][j] << " ";
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
std::cout << std::endl
|
||||||
|
<< "Value of the Gaussian Elimination method: " << std::endl;
|
||||||
|
for (i = mat_size - 1; i >= 0; i--) {
|
||||||
|
double sum = 0;
|
||||||
|
for (j = mat_size - 1; j > i; j--) {
|
||||||
|
x[i][j] = x[j][j] * x[i][j];
|
||||||
|
sum = x[i][j] + sum;
|
||||||
|
}
|
||||||
|
if (x[i][i] == 0)
|
||||||
|
x[i][i] = 0;
|
||||||
|
else
|
||||||
|
x[i][i] = (x[i][mat_size] - sum) / (x[i][i]);
|
||||||
|
|
||||||
|
std::cout << "x" << i << "= " << x[i][i] << std::endl;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
#include <cmath>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
static float eq(float i) {
|
||||||
|
return (std::pow(i, 3) - (4 * i) - 9); // original equation
|
||||||
|
}
|
||||||
|
|
||||||
|
static float eq_der(float i) {
|
||||||
|
return ((3 * std::pow(i, 2)) - 4); // derivative of equation
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
float a, b, z, c, m, n;
|
||||||
|
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
z = eq(i);
|
||||||
|
if (z >= 0) {
|
||||||
|
b = i;
|
||||||
|
a = --i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "\nFirst initial: " << a;
|
||||||
|
std::cout << "\nSecond initial: " << b;
|
||||||
|
c = (a + b) / 2;
|
||||||
|
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
float h;
|
||||||
|
m = eq(c);
|
||||||
|
n = eq_der(c);
|
||||||
|
|
||||||
|
z = c - (m / n);
|
||||||
|
c = z;
|
||||||
|
|
||||||
|
if (m > 0 && m < 0.009) // stoping criteria
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "\n\nRoot: " << z << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
@ -1,12 +1,17 @@
|
|||||||
/**
|
/**
|
||||||
|
* @file
|
||||||
|
*
|
||||||
* Program that gets the number of data samples and number of features per
|
* Program that gets the number of data samples and number of features per
|
||||||
* sample along with output per sample. It applies OLS regression to compute
|
* sample along with output per sample. It applies OLS regression to compute
|
||||||
* the regression output for additional test data samples.
|
* the regression output for additional test data samples.
|
||||||
**/
|
*/
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* operator to print a matrix
|
||||||
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::ostream &operator<<(std::ostream &out,
|
std::ostream &operator<<(std::ostream &out,
|
||||||
std::vector<std::vector<T>> const &v) {
|
std::vector<std::vector<T>> const &v) {
|
||||||
@ -23,29 +28,40 @@ std::ostream &operator<<(std::ostream &out,
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* operator to print a vector
|
||||||
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::ostream &operator<<(std::ostream &out, std::vector<T> const &v) {
|
std::ostream &operator<<(std::ostream &out, std::vector<T> const &v) {
|
||||||
const int width = 15;
|
const int width = 15;
|
||||||
const char separator = ' ';
|
const char separator = ' ';
|
||||||
|
|
||||||
for (size_t row = 0; row < v.size(); row++)
|
for (size_t row = 0; row < v.size(); row++)
|
||||||
out << std::left << std::setw(width) << std::setfill(separator) << v[row];
|
out << std::left << std::setw(width) << std::setfill(separator)
|
||||||
|
<< v[row];
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* function to check if given matrix is a square matrix
|
||||||
|
* \returns 1 if true, 0 if false
|
||||||
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline bool is_square(std::vector<std::vector<T>> const &A) {
|
inline bool is_square(std::vector<std::vector<T>> const &A) {
|
||||||
// Assuming A is square matrix
|
// Assuming A is square matrix
|
||||||
size_t N = A.size();
|
size_t N = A.size();
|
||||||
for (size_t i = 0; i < N; i++)
|
for (size_t i = 0; i < N; i++)
|
||||||
if (A[i].size() != N)
|
if (A[i].size() != N) return false;
|
||||||
return false;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* matrix multiplication
|
* Matrix multiplication such that if A is size (mxn) and
|
||||||
|
* B is of size (pxq) then the multiplication is defined
|
||||||
|
* only when n = p and the resultant matrix is of size (mxq)
|
||||||
|
*
|
||||||
|
* \returns resultant matrix
|
||||||
**/
|
**/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::vector<std::vector<T>> operator*(std::vector<std::vector<T>> const &A,
|
std::vector<std::vector<T>> operator*(std::vector<std::vector<T>> const &A,
|
||||||
@ -76,6 +92,10 @@ std::vector<std::vector<T>> operator*(std::vector<std::vector<T>> const &A,
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* multiplication of a matrix with a column vector
|
||||||
|
* \returns resultant vector
|
||||||
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::vector<T> operator*(std::vector<std::vector<T>> const &A,
|
std::vector<T> operator*(std::vector<std::vector<T>> const &A,
|
||||||
std::vector<T> const &B) {
|
std::vector<T> const &B) {
|
||||||
@ -92,13 +112,16 @@ std::vector<T> operator*(std::vector<std::vector<T>> const &A,
|
|||||||
|
|
||||||
for (size_t row = 0; row < N_A; row++) {
|
for (size_t row = 0; row < N_A; row++) {
|
||||||
result[row] = static_cast<T>(0);
|
result[row] = static_cast<T>(0);
|
||||||
for (size_t j = 0; j < B.size(); j++)
|
for (size_t j = 0; j < B.size(); j++) result[row] += A[row][j] * B[j];
|
||||||
result[row] += A[row][j] * B[j];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* pre-multiplication of a vector by a scalar
|
||||||
|
* \returns resultant vector
|
||||||
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::vector<float> operator*(float const scalar, std::vector<T> const &A) {
|
std::vector<float> operator*(float const scalar, std::vector<T> const &A) {
|
||||||
// Number of rows in A
|
// Number of rows in A
|
||||||
@ -113,6 +136,10 @@ std::vector<float> operator*(float const scalar, std::vector<T> const &A) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* post-multiplication of a vector by a scalar
|
||||||
|
* \returns resultant vector
|
||||||
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::vector<float> operator*(std::vector<T> const &A, float const scalar) {
|
std::vector<float> operator*(std::vector<T> const &A, float const scalar) {
|
||||||
// Number of rows in A
|
// Number of rows in A
|
||||||
@ -126,11 +153,19 @@ std::vector<float> operator*(std::vector<T> const &A, float const scalar) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* division of a vector by a scalar
|
||||||
|
* \returns resultant vector
|
||||||
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::vector<float> operator/(std::vector<T> const &A, float const scalar) {
|
std::vector<float> operator/(std::vector<T> const &A, float const scalar) {
|
||||||
return (1.f / scalar) * A;
|
return (1.f / scalar) * A;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* subtraction of two vectors of identical lengths
|
||||||
|
* \returns resultant vector
|
||||||
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::vector<T> operator-(std::vector<T> const &A, std::vector<T> const &B) {
|
std::vector<T> operator-(std::vector<T> const &A, std::vector<T> const &B) {
|
||||||
// Number of rows in A
|
// Number of rows in A
|
||||||
@ -143,12 +178,15 @@ std::vector<T> operator-(std::vector<T> const &A, std::vector<T> const &B) {
|
|||||||
return A;
|
return A;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t row = 0; row < N; row++)
|
for (size_t row = 0; row < N; row++) result[row] = A[row] - B[row];
|
||||||
result[row] = A[row] - B[row];
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* addition of two vectors of identical lengths
|
||||||
|
* \returns resultant vector
|
||||||
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::vector<T> operator+(std::vector<T> const &A, std::vector<T> const &B) {
|
std::vector<T> operator+(std::vector<T> const &A, std::vector<T> const &B) {
|
||||||
// Number of rows in A
|
// Number of rows in A
|
||||||
@ -161,18 +199,19 @@ std::vector<T> operator+(std::vector<T> const &A, std::vector<T> const &B) {
|
|||||||
return A;
|
return A;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t row = 0; row < N; row++)
|
for (size_t row = 0; row < N; row++) result[row] = A[row] + B[row];
|
||||||
result[row] = A[row] + B[row];
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get matrix inverse using Row-trasnformations
|
* Get matrix inverse using Row-trasnformations. Given matrix must
|
||||||
|
* be a square and non-singular.
|
||||||
|
* \returns inverse matrix
|
||||||
**/
|
**/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::vector<std::vector<float>>
|
std::vector<std::vector<float>> get_inverse(
|
||||||
get_inverse(std::vector<std::vector<T>> const &A) {
|
std::vector<std::vector<T>> const &A) {
|
||||||
// Assuming A is square matrix
|
// Assuming A is square matrix
|
||||||
size_t N = A.size();
|
size_t N = A.size();
|
||||||
|
|
||||||
@ -226,8 +265,7 @@ get_inverse(std::vector<std::vector<T>> const &A) {
|
|||||||
inverse[row] = inverse[row] / divisor;
|
inverse[row] = inverse[row] / divisor;
|
||||||
// Row transformations
|
// Row transformations
|
||||||
for (size_t row2 = 0; row2 < N; row2++) {
|
for (size_t row2 = 0; row2 < N; row2++) {
|
||||||
if (row2 == row)
|
if (row2 == row) continue;
|
||||||
continue;
|
|
||||||
float factor = temp[row2][row];
|
float factor = temp[row2][row];
|
||||||
temp[row2] = temp[row2] - factor * temp[row];
|
temp[row2] = temp[row2] - factor * temp[row];
|
||||||
inverse[row2] = inverse[row2] - factor * inverse[row];
|
inverse[row2] = inverse[row2] - factor * inverse[row];
|
||||||
@ -239,22 +277,29 @@ get_inverse(std::vector<std::vector<T>> const &A) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* matrix transpose
|
* matrix transpose
|
||||||
|
* \returns resultant matrix
|
||||||
**/
|
**/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::vector<std::vector<T>>
|
std::vector<std::vector<T>> get_transpose(
|
||||||
get_transpose(std::vector<std::vector<T>> const &A) {
|
std::vector<std::vector<T>> const &A) {
|
||||||
std::vector<std::vector<T>> result(A[0].size());
|
std::vector<std::vector<T>> result(A[0].size());
|
||||||
|
|
||||||
for (size_t row = 0; row < A[0].size(); row++) {
|
for (size_t row = 0; row < A[0].size(); row++) {
|
||||||
std::vector<T> v(A.size());
|
std::vector<T> v(A.size());
|
||||||
for (size_t col = 0; col < A.size(); col++)
|
for (size_t col = 0; col < A.size(); col++) v[col] = A[col][row];
|
||||||
v[col] = A[col][row];
|
|
||||||
|
|
||||||
result[row] = v;
|
result[row] = v;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Perform Ordinary Least Squares curve fit. This operation is defined as
|
||||||
|
* \f[\beta = \left(X^TXX^T\right)Y\f]
|
||||||
|
* \param X feature matrix with rows representing sample vector of features
|
||||||
|
* \param Y known regression value for each sample
|
||||||
|
* \returns fitted regression model polynomial coefficients
|
||||||
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::vector<float> fit_OLS_regressor(std::vector<std::vector<T>> const &X,
|
std::vector<float> fit_OLS_regressor(std::vector<std::vector<T>> const &X,
|
||||||
std::vector<T> const &Y) {
|
std::vector<T> const &Y) {
|
||||||
@ -278,11 +323,17 @@ std::vector<float> fit_OLS_regressor(std::vector<std::vector<T>> const &X,
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Given data and OLS model coeffficients, predict
|
* Given data and OLS model coeffficients, predict
|
||||||
* regression estimates
|
* regression estimates. This operation is defined as
|
||||||
|
* \f[y_{\text{row}=i} = \sum_{j=\text{columns}}\beta_j\cdot X_{i,j}\f]
|
||||||
|
*
|
||||||
|
* \param X feature matrix with rows representing sample vector of features
|
||||||
|
* \param beta fitted regression model
|
||||||
|
* \return vector with regression values for each sample
|
||||||
**/
|
**/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::vector<float> predict_OLS_regressor(std::vector<std::vector<T>> const &X,
|
std::vector<float> predict_OLS_regressor(std::vector<std::vector<T>> const &X,
|
||||||
std::vector<float> const &beta) {
|
std::vector<float> const &beta /**< */
|
||||||
|
) {
|
||||||
std::vector<float> result(X.size());
|
std::vector<float> result(X.size());
|
||||||
|
|
||||||
for (size_t rows = 0; rows < X.size(); rows++) {
|
for (size_t rows = 0; rows < X.size(); rows++) {
|
||||||
@ -295,6 +346,9 @@ std::vector<float> predict_OLS_regressor(std::vector<std::vector<T>> const &X,
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* main function
|
||||||
|
*/
|
||||||
int main() {
|
int main() {
|
||||||
size_t N, F;
|
size_t N, F;
|
||||||
|
|
||||||
@ -336,14 +390,12 @@ int main() {
|
|||||||
for (size_t rows = 0; rows < T; rows++) {
|
for (size_t rows = 0; rows < T; rows++) {
|
||||||
std::cout << "Sample# " << rows + 1 << ": ";
|
std::cout << "Sample# " << rows + 1 << ": ";
|
||||||
std::vector<float> v(F);
|
std::vector<float> v(F);
|
||||||
for (size_t cols = 0; cols < F; cols++)
|
for (size_t cols = 0; cols < F; cols++) std::cin >> v[cols];
|
||||||
std::cin >> v[cols];
|
|
||||||
data2[rows] = v;
|
data2[rows] = v;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<float> out = predict_OLS_regressor(data2, beta);
|
std::vector<float> out = predict_OLS_regressor(data2, beta);
|
||||||
for (size_t rows = 0; rows < T; rows++)
|
for (size_t rows = 0; rows < T; rows++) std::cout << out[rows] << std::endl;
|
||||||
std::cout << out[rows] << std::endl;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
37
computer_oriented_statistical_methods/secant_method.cpp
Normal file
37
computer_oriented_statistical_methods/secant_method.cpp
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#include <cmath>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
static float eq(float i) {
|
||||||
|
return (pow(i, 3) - (4 * i) - 9); // original equation
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
float a, b, z, c, m, n;
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
z = eq(i);
|
||||||
|
if (z >= 0) {
|
||||||
|
b = i;
|
||||||
|
a = --i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "\nFirst initial: " << a;
|
||||||
|
std::cout << "\nSecond initial: " << b;
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
float h, d;
|
||||||
|
m = eq(a);
|
||||||
|
n = eq(b);
|
||||||
|
|
||||||
|
c = ((a * n) - (b * m)) / (n - m);
|
||||||
|
a = b;
|
||||||
|
b = c;
|
||||||
|
|
||||||
|
z = eq(c);
|
||||||
|
if (z > 0 && z < 0.09) // stoping criteria
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "\n\nRoot: " << c;
|
||||||
|
return 0;
|
||||||
|
}
|
@ -1,37 +0,0 @@
|
|||||||
#include <conio.h>
|
|
||||||
#include <iostream.h>
|
|
||||||
#include <math.h>
|
|
||||||
float eq(float y)
|
|
||||||
{
|
|
||||||
return ((3 * y) - (cos(y)) - 2);
|
|
||||||
}
|
|
||||||
float eqd(float y)
|
|
||||||
{
|
|
||||||
return ((0.5) * ((cos(y)) + 2));
|
|
||||||
}
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
float y, x1, x2, x3, sum, s, a, f1, f2, gd;
|
|
||||||
int i, n;
|
|
||||||
|
|
||||||
clrscr();
|
|
||||||
for (i = 0; i < 10; i++)
|
|
||||||
{
|
|
||||||
sum = eq(y);
|
|
||||||
cout << "value of equation at " << i << " " << sum << "\n";
|
|
||||||
y++;
|
|
||||||
}
|
|
||||||
cout << "enter the x1->";
|
|
||||||
cin >> x1;
|
|
||||||
cout << "enter the no iteration to perform->\n";
|
|
||||||
cin >> n;
|
|
||||||
|
|
||||||
for (i = 0; i <= n; i++)
|
|
||||||
{
|
|
||||||
x2 = eqd(x1);
|
|
||||||
cout << "\nenter the x2->" << x2;
|
|
||||||
x1 = x2;
|
|
||||||
}
|
|
||||||
getch();
|
|
||||||
}
|
|
@ -0,0 +1,27 @@
|
|||||||
|
#include <cmath>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
static float eq(float y) { return ((3 * y) - (cos(y)) - 2); }
|
||||||
|
static float eqd(float y) { return ((0.5) * ((cos(y)) + 2)); }
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
float y, x1, x2, x3, sum, s, a, f1, f2, gd;
|
||||||
|
int i, n;
|
||||||
|
|
||||||
|
for (i = 0; i < 10; i++) {
|
||||||
|
sum = eq(y);
|
||||||
|
std::cout << "value of equation at " << i << " " << sum << "\n";
|
||||||
|
y++;
|
||||||
|
}
|
||||||
|
std::cout << "enter the x1->";
|
||||||
|
std::cin >> x1;
|
||||||
|
std::cout << "enter the no iteration to perform->\n";
|
||||||
|
std::cin >> n;
|
||||||
|
|
||||||
|
for (i = 0; i <= n; i++) {
|
||||||
|
x2 = eqd(x1);
|
||||||
|
std::cout << "\nenter the x2->" << x2;
|
||||||
|
x1 = x2;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
18
math/CMakeLists.txt
Normal file
18
math/CMakeLists.txt
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
|
||||||
|
# with full pathname. RELATIVE may makes it easier to extract an executable name
|
||||||
|
# automatically.
|
||||||
|
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp )
|
||||||
|
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
|
||||||
|
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
|
||||||
|
foreach( testsourcefile ${APP_SOURCES} )
|
||||||
|
# I used a simple string replace, to cut off .cpp.
|
||||||
|
string( REPLACE ".cpp" "" testname ${testsourcefile} )
|
||||||
|
add_executable( ${testname} ${testsourcefile} )
|
||||||
|
|
||||||
|
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX)
|
||||||
|
if(OpenMP_CXX_FOUND)
|
||||||
|
target_link_libraries(${testname} OpenMP::OpenMP_CXX)
|
||||||
|
endif()
|
||||||
|
install(TARGETS ${testname} DESTINATION "bin/math")
|
||||||
|
|
||||||
|
endforeach( testsourcefile ${APP_SOURCES} )
|
@ -1,5 +1,5 @@
|
|||||||
#include <iostream>
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
/* Calculate the the value on Fibonacci's sequence given an
|
/* Calculate the the value on Fibonacci's sequence given an
|
||||||
integer as input
|
integer as input
|
||||||
@ -7,11 +7,10 @@ Fibonacci = 0, 1, 1, 2, 3, 5,
|
|||||||
8, 13, 21, 34, 55,
|
8, 13, 21, 34, 55,
|
||||||
89, 144, ... */
|
89, 144, ... */
|
||||||
|
|
||||||
int fibonacci(uint n) {
|
int fibonacci(unsigned int n) {
|
||||||
/* If the input is 0 or 1 just return the same
|
/* If the input is 0 or 1 just return the same
|
||||||
This will set the first 2 values of the sequence */
|
This will set the first 2 values of the sequence */
|
||||||
if (n <= 1)
|
if (n <= 1) return n;
|
||||||
return n;
|
|
||||||
|
|
||||||
/* Add the last 2 values of the sequence to get next */
|
/* Add the last 2 values of the sequence to get next */
|
||||||
return fibonacci(n - 1) + fibonacci(n - 2);
|
return fibonacci(n - 1) + fibonacci(n - 2);
|
||||||
|
@ -1,80 +1,67 @@
|
|||||||
|
#include <algorithm>
|
||||||
|
#include <cstring>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <algorithm>
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
// Declaring variables for maintaing prime numbers and to check whether a number is prime or not
|
// Declaring variables for maintaing prime numbers and to check whether a number
|
||||||
|
// is prime or not
|
||||||
bool isprime[1000006];
|
bool isprime[1000006];
|
||||||
vector<int> prime_numbers;
|
std::vector<int> prime_numbers;
|
||||||
vector<pair<int, int>> factors;
|
std::vector<std::pair<int, int>> factors;
|
||||||
|
|
||||||
// Calculating prime number upto a given range
|
// Calculating prime number upto a given range
|
||||||
void SieveOfEratosthenes(int N)
|
void SieveOfEratosthenes(int N) {
|
||||||
{
|
|
||||||
// initializes the array isprime
|
// initializes the array isprime
|
||||||
memset(isprime, true, sizeof isprime);
|
memset(isprime, true, sizeof isprime);
|
||||||
|
|
||||||
for (int i = 2; i <= N; i++)
|
for (int i = 2; i <= N; i++) {
|
||||||
{
|
if (isprime[i]) {
|
||||||
if (isprime[i])
|
for (int j = 2 * i; j <= N; j += i) isprime[j] = false;
|
||||||
{
|
|
||||||
for (int j = 2 * i; j <= N; j += i)
|
|
||||||
isprime[j] = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 2; i <= N; i++)
|
for (int i = 2; i <= N; i++) {
|
||||||
{
|
if (isprime[i]) prime_numbers.push_back(i);
|
||||||
if (isprime[i])
|
|
||||||
prime_numbers.push_back(i);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prime factorization of a number
|
// Prime factorization of a number
|
||||||
void prime_factorization(int num)
|
void prime_factorization(int num) {
|
||||||
{
|
|
||||||
|
|
||||||
int number = num;
|
int number = num;
|
||||||
|
|
||||||
for (int i = 0; prime_numbers[i] <= num; i++)
|
for (int i = 0; prime_numbers[i] <= num; i++) {
|
||||||
{
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
// termination condition
|
// termination condition
|
||||||
if (number == 1)
|
if (number == 1) {
|
||||||
{
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (number % prime_numbers[i] == 0)
|
while (number % prime_numbers[i] == 0) {
|
||||||
{
|
|
||||||
count++;
|
count++;
|
||||||
number = number / prime_numbers[i];
|
number = number / prime_numbers[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count)
|
if (count) factors.push_back(std::make_pair(prime_numbers[i], count));
|
||||||
factors.push_back(make_pair(prime_numbers[i], count));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
I added a simple UI.
|
I added a simple UI.
|
||||||
*/
|
*/
|
||||||
int main()
|
int main() {
|
||||||
{
|
|
||||||
int num;
|
int num;
|
||||||
cout << "\t\tComputes the prime factorization\n\n";
|
std::cout << "\t\tComputes the prime factorization\n\n";
|
||||||
cout << "Type in a number: ";
|
std::cout << "Type in a number: ";
|
||||||
cin >> num;
|
std::cin >> num;
|
||||||
|
|
||||||
SieveOfEratosthenes(num);
|
SieveOfEratosthenes(num);
|
||||||
|
|
||||||
prime_factorization(num);
|
prime_factorization(num);
|
||||||
|
|
||||||
// Prime factors with their powers in the given number in new line
|
// Prime factors with their powers in the given number in new line
|
||||||
for (auto it : factors)
|
for (auto it : factors) {
|
||||||
{
|
std::cout << it.first << " " << it.second << std::endl;
|
||||||
cout << it.first << " " << it.second << endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
#include<iostream>
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
char prime[100000000];
|
char prime[100000000];
|
||||||
|
|
||||||
@ -15,7 +15,6 @@ void Sieve(int64_t n) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Sieve(100000000);
|
Sieve(100000000);
|
||||||
int64_t n;
|
int64_t n;
|
18
others/CMakeLists.txt
Normal file
18
others/CMakeLists.txt
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
|
||||||
|
# with full pathname. RELATIVE may makes it easier to extract an executable name
|
||||||
|
# automatically.
|
||||||
|
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp )
|
||||||
|
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
|
||||||
|
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
|
||||||
|
foreach( testsourcefile ${APP_SOURCES} )
|
||||||
|
# I used a simple string replace, to cut off .cpp.
|
||||||
|
string( REPLACE ".cpp" "" testname ${testsourcefile} )
|
||||||
|
add_executable( ${testname} ${testsourcefile} )
|
||||||
|
|
||||||
|
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX)
|
||||||
|
if(OpenMP_CXX_FOUND)
|
||||||
|
target_link_libraries(${testname} OpenMP::OpenMP_CXX)
|
||||||
|
endif()
|
||||||
|
install(TARGETS ${testname} DESTINATION "bin/others")
|
||||||
|
|
||||||
|
endforeach( testsourcefile ${APP_SOURCES} )
|
@ -1,23 +0,0 @@
|
|||||||
//This program aims at calculating the GCD of n numbers by division method
|
|
||||||
#include <iostream>
|
|
||||||
using namepsace std;
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
cout << "Enter value of n:" << endl;
|
|
||||||
cin >> n;
|
|
||||||
int a[n];
|
|
||||||
int i, j, gcd;
|
|
||||||
cout << "Enter the n numbers:" << endl;
|
|
||||||
for (i = 0; i < n; i++)
|
|
||||||
cin >> a[i];
|
|
||||||
j = 1; //to access all elements of the array starting from 1
|
|
||||||
gcd = a[0];
|
|
||||||
while (j < n)
|
|
||||||
{
|
|
||||||
if (a[j] % gcd == 0) //value of gcd is as needed so far
|
|
||||||
j++; //so we check for next element
|
|
||||||
else
|
|
||||||
gcd = a[j] % gcd; //calculating GCD by division method
|
|
||||||
}
|
|
||||||
cout << "GCD of entered n numbers:" << gcd;
|
|
||||||
}
|
|
@ -1,56 +0,0 @@
|
|||||||
#include <iostream>
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
Multiply(int A[][], int B[][], int n)
|
|
||||||
{
|
|
||||||
if (n == 2)
|
|
||||||
{
|
|
||||||
int p1 = (a[0][0] + a[1][1]) * (b[0][0] + b[1][1]);
|
|
||||||
int p2 = (a[1][0] + a[1][1]) * b[0][0];
|
|
||||||
int p3 = a[0][0] * (b[0][1] - b[1][1]);
|
|
||||||
int p4 = a[1][1] * (b[1][0] - b[0][0]);
|
|
||||||
int p5 = (a[0][0] + a[0][1]) * b[1][1];
|
|
||||||
int p6 = (a[1][0] - a[0][0]) * (b[0][0] + b[0][1]);
|
|
||||||
int p7 = (a[0][1] - a[1][1]) * (b[1][0] + b[1][1]);
|
|
||||||
|
|
||||||
int c[n][n];
|
|
||||||
c[0][0] = p1 + p4 - p5 + p7;
|
|
||||||
c[0][1] = p3 + p5;
|
|
||||||
c[1][0] = p2 + p4;
|
|
||||||
c[1][1] = p1 - p2 + p3 + p6;
|
|
||||||
|
|
||||||
return c[][];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
int p, q, r, s;
|
|
||||||
cout << "Enter the dimensions of Matrices";
|
|
||||||
cin >> n;
|
|
||||||
int A[n][n], ;
|
|
||||||
int B[n][n], ;
|
|
||||||
cout << "Enter the elements of Matrix A";
|
|
||||||
for (int i = 0; i < n; i++)
|
|
||||||
{
|
|
||||||
for (int j = 0; j < n; j++)
|
|
||||||
{
|
|
||||||
cin >> A[i][j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cout << "Enter the elements of Matrix B";
|
|
||||||
for (int i = 0; i < n; i++)
|
|
||||||
{
|
|
||||||
for (int j = 0; j < n; j++)
|
|
||||||
{
|
|
||||||
cin >> B[i][j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Multiply(A, B, n);
|
|
||||||
return 0;
|
|
||||||
}
|
|
@ -1,42 +0,0 @@
|
|||||||
//An efficient way to calculate nth fibonacci number faster and simpler than O(nlogn) method of matrix exponentiation
|
|
||||||
//This works by using both recursion and dynamic programming.
|
|
||||||
//as 93rd fibonacci exceeds 19 digits, which cannot be stored in a single long long variable, we can only use it till 92nd fibonacci
|
|
||||||
//we can use it for 10000th fibonacci etc, if we implement bigintegers.
|
|
||||||
//This algorithm works with the fact that nth fibonacci can easily found if we have already found n/2th or (n+1)/2th fibonacci
|
|
||||||
//It is a property of fibonacci similar to matrix exponentiation.
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <cstdio>
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
const long long MAX = 93;
|
|
||||||
|
|
||||||
long long f[MAX] = {0};
|
|
||||||
|
|
||||||
long long fib(long long n)
|
|
||||||
{
|
|
||||||
|
|
||||||
if (n == 0)
|
|
||||||
return 0;
|
|
||||||
if (n == 1 || n == 2)
|
|
||||||
return (f[n] = 1);
|
|
||||||
|
|
||||||
if (f[n])
|
|
||||||
return f[n];
|
|
||||||
|
|
||||||
long long k = (n % 2 != 0) ? (n + 1) / 2 : n / 2;
|
|
||||||
|
|
||||||
f[n] = (n % 2 != 0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1))
|
|
||||||
: (2 * fib(k - 1) + fib(k)) * fib(k);
|
|
||||||
return f[n];
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
//Main Function
|
|
||||||
for (long long i = 1; i < 93; i++)
|
|
||||||
{
|
|
||||||
cout << i << " th fibonacci number is " << fib(i) << "\n";
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
37
others/fibonacci_fast.cpp
Normal file
37
others/fibonacci_fast.cpp
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
// An efficient way to calculate nth fibonacci number faster and simpler than
|
||||||
|
// O(nlogn) method of matrix exponentiation This works by using both recursion
|
||||||
|
// and dynamic programming. as 93rd fibonacci exceeds 19 digits, which cannot be
|
||||||
|
// stored in a single long long variable, we can only use it till 92nd fibonacci
|
||||||
|
// we can use it for 10000th fibonacci etc, if we implement bigintegers.
|
||||||
|
// This algorithm works with the fact that nth fibonacci can easily found if we
|
||||||
|
// have already found n/2th or (n+1)/2th fibonacci It is a property of fibonacci
|
||||||
|
// similar to matrix exponentiation.
|
||||||
|
|
||||||
|
#include <cinttypes>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
const uint64_t MAX = 93;
|
||||||
|
|
||||||
|
uint64_t f[MAX] = {0};
|
||||||
|
|
||||||
|
uint64_t fib(uint64_t n) {
|
||||||
|
if (n == 0) return 0;
|
||||||
|
if (n == 1 || n == 2) return (f[n] = 1);
|
||||||
|
|
||||||
|
if (f[n]) return f[n];
|
||||||
|
|
||||||
|
uint64_t k = (n % 2 != 0) ? (n + 1) / 2 : n / 2;
|
||||||
|
|
||||||
|
f[n] = (n % 2 != 0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1))
|
||||||
|
: (2 * fib(k - 1) + fib(k)) * fib(k);
|
||||||
|
return f[n];
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// Main Function
|
||||||
|
for (uint64_t i = 1; i < 93; i++) {
|
||||||
|
std::cout << i << " th fibonacci number is " << fib(i) << "\n";
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
23
others/gcd_of_n_numbers.cpp
Normal file
23
others/gcd_of_n_numbers.cpp
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
// This program aims at calculating the GCD of n numbers by division method
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int n;
|
||||||
|
std::cout << "Enter value of n:" << std::endl;
|
||||||
|
std::cin >> n;
|
||||||
|
int *a = new int[n];
|
||||||
|
int i, j, gcd;
|
||||||
|
std::cout << "Enter the n numbers:" << std::endl;
|
||||||
|
for (i = 0; i < n; i++) std::cin >> a[i];
|
||||||
|
j = 1; // to access all elements of the array starting from 1
|
||||||
|
gcd = a[0];
|
||||||
|
while (j < n) {
|
||||||
|
if (a[j] % gcd == 0) // value of gcd is as needed so far
|
||||||
|
j++; // so we check for next element
|
||||||
|
else
|
||||||
|
gcd = a[j] % gcd; // calculating GCD by division method
|
||||||
|
}
|
||||||
|
std::cout << "GCD of entered n numbers:" << gcd;
|
||||||
|
delete[] a;
|
||||||
|
return 0;
|
||||||
|
}
|
@ -19,6 +19,8 @@ The first element of this matrix is the required result.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
using std::cin;
|
using std::cin;
|
||||||
using std::cout;
|
using std::cout;
|
||||||
using std::vector;
|
using std::vector;
|
||||||
@ -46,8 +48,7 @@ vector<vector<ll>> multiply(vector<vector<ll>> A, vector<vector<ll>> B) {
|
|||||||
|
|
||||||
// computing power of a matrix
|
// computing power of a matrix
|
||||||
vector<vector<ll>> power(vector<vector<ll>> A, ll p) {
|
vector<vector<ll>> power(vector<vector<ll>> A, ll p) {
|
||||||
if (p == 1)
|
if (p == 1) return A;
|
||||||
return A;
|
|
||||||
if (p % 2 == 1) {
|
if (p % 2 == 1) {
|
||||||
return multiply(A, power(A, p - 1));
|
return multiply(A, power(A, p - 1));
|
||||||
} else {
|
} else {
|
||||||
@ -58,14 +59,11 @@ vector<vector<ll>> power(vector<vector<ll>> A, ll p) {
|
|||||||
|
|
||||||
// main function
|
// main function
|
||||||
ll ans(ll n) {
|
ll ans(ll n) {
|
||||||
if (n == 0)
|
if (n == 0) return 0;
|
||||||
return 0;
|
if (n <= k) return b[n - 1];
|
||||||
if (n <= k)
|
|
||||||
return b[n - 1];
|
|
||||||
// F1
|
// F1
|
||||||
vector<ll> F1(k + 1);
|
vector<ll> F1(k + 1);
|
||||||
for (ll i = 1; i <= k; i++)
|
for (ll i = 1; i <= k; i++) F1[i] = b[i - 1];
|
||||||
F1[i] = b[i - 1];
|
|
||||||
|
|
||||||
// Transpose matrix
|
// Transpose matrix
|
||||||
vector<vector<ll>> T(k + 1, vector<ll>(k + 1));
|
vector<vector<ll>> T(k + 1, vector<ll>(k + 1));
|
||||||
|
@ -1,29 +1,22 @@
|
|||||||
|
#include <cstring>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
using namespace std;
|
void show_pascal(int **arr, int n) {
|
||||||
|
|
||||||
void show_pascal(int **arr, int n)
|
|
||||||
{
|
|
||||||
// pint Pascal's Triangle
|
// pint Pascal's Triangle
|
||||||
for (int i = 0; i < n; ++i)
|
for (int i = 0; i < n; ++i) {
|
||||||
{
|
for (int j = 0; j < n + i; ++j) {
|
||||||
for (int j = 0; j < n + i; ++j)
|
|
||||||
{
|
|
||||||
if (arr[i][j] == 0)
|
if (arr[i][j] == 0)
|
||||||
cout << " ";
|
std::cout << " ";
|
||||||
else
|
else
|
||||||
cout << arr[i][j];
|
std::cout << arr[i][j];
|
||||||
}
|
}
|
||||||
cout << endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int **pascal_triangle(int **arr, int n)
|
int **pascal_triangle(int **arr, int n) {
|
||||||
{
|
for (int i = 0; i < n; ++i) {
|
||||||
for (int i = 0; i < n; ++i)
|
for (int j = n - i - 1; j < n + i; ++j) {
|
||||||
{
|
|
||||||
for (int j = n - i - 1; j < n + i; ++j)
|
|
||||||
{
|
|
||||||
if (j == n - i - 1 || j == n + i - 1)
|
if (j == n - i - 1 || j == n + i - 1)
|
||||||
arr[i][j] = 1; // The edge of the Pascal triangle goes in 1
|
arr[i][j] = 1; // The edge of the Pascal triangle goes in 1
|
||||||
else
|
else
|
||||||
@ -34,17 +27,15 @@ int **pascal_triangle(int **arr, int n)
|
|||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
|
||||||
int n = 0;
|
int n = 0;
|
||||||
|
|
||||||
cout << "Set Pascal's Triangle Height" << endl;
|
std::cout << "Set Pascal's Triangle Height" << std::endl;
|
||||||
cin >> n;
|
std::cin >> n;
|
||||||
|
|
||||||
// memory allocation (Assign two-dimensional array to store Pascal triangle)
|
// memory allocation (Assign two-dimensional array to store Pascal triangle)
|
||||||
int **arr = new int *[n];
|
int **arr = new int *[n];
|
||||||
for (int i = 0; i < n; ++i)
|
for (int i = 0; i < n; ++i) {
|
||||||
{
|
|
||||||
arr[i] = new int[2 * n - 1];
|
arr[i] = new int[2 * n - 1];
|
||||||
memset(arr[i], 0, sizeof(int) * (2 * n - 1));
|
memset(arr[i], 0, sizeof(int) * (2 * n - 1));
|
||||||
}
|
}
|
||||||
@ -53,8 +44,7 @@ int main()
|
|||||||
show_pascal(arr, n);
|
show_pascal(arr, n);
|
||||||
|
|
||||||
// deallocation
|
// deallocation
|
||||||
for (int i = 0; i < n; ++i)
|
for (int i = 0; i < n; ++i) {
|
||||||
{
|
|
||||||
delete[] arr[i];
|
delete[] arr[i];
|
||||||
}
|
}
|
||||||
delete[] arr;
|
delete[] arr;
|
||||||
|
105
sorting/shell_sort2.cpp
Normal file
105
sorting/shell_sort2.cpp
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
#include <array>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <ctime>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
// for std::swap
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
template <class T> void show_data(T *arr, size_t LEN) {
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
for (i = 0; i < LEN; i++)
|
||||||
|
std::cout << arr[i] << ", ";
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T, size_t N> void show_data(T (&arr)[N]) { show_data(arr, N); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Optimized algorithm - takes half the time by utilizing
|
||||||
|
* Mar
|
||||||
|
**/
|
||||||
|
template <class T> void shell_sort(T *arr, size_t LEN) {
|
||||||
|
const unsigned int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
|
||||||
|
const unsigned int gap_len = 8;
|
||||||
|
size_t i, j, g;
|
||||||
|
|
||||||
|
for (g = 0; g < gap_len; g++) {
|
||||||
|
unsigned int gap = gaps[g];
|
||||||
|
for (i = gap; i < LEN; i++) {
|
||||||
|
T tmp = arr[i];
|
||||||
|
|
||||||
|
for (j = i; j >= gap && (arr[j - gap] - tmp) > 0; j -= gap)
|
||||||
|
arr[j] = arr[j - gap];
|
||||||
|
|
||||||
|
arr[j] = tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T, size_t N> void shell_sort(T (&arr)[N]) {
|
||||||
|
shell_sort(arr, N);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* function to compare sorting using cstdlib's qsort
|
||||||
|
**/
|
||||||
|
int compare(const void *a, const void *b) {
|
||||||
|
int arg1 = *static_cast<const int *>(a);
|
||||||
|
int arg2 = *static_cast<const int *>(b);
|
||||||
|
|
||||||
|
if (arg1 < arg2)
|
||||||
|
return -1;
|
||||||
|
if (arg1 > arg2)
|
||||||
|
return 1;
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
// return (arg1 > arg2) - (arg1 < arg2); // possible shortcut
|
||||||
|
// return arg1 - arg2; // erroneous shortcut (fails if INT_MIN is present)
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
int i, NUM_DATA;
|
||||||
|
|
||||||
|
if (argc == 2)
|
||||||
|
NUM_DATA = atoi(argv[1]);
|
||||||
|
else
|
||||||
|
NUM_DATA = 200;
|
||||||
|
|
||||||
|
// int array = new int[NUM_DATA];
|
||||||
|
int *data = new int[NUM_DATA];
|
||||||
|
int *data2 = new int[NUM_DATA];
|
||||||
|
// int array2 = new int[NUM_DATA];
|
||||||
|
int range = 1800;
|
||||||
|
|
||||||
|
std::srand(time(NULL));
|
||||||
|
for (i = 0; i < NUM_DATA; i++)
|
||||||
|
data[i] = data2[i] = (std::rand() % range) - (range >> 1);
|
||||||
|
|
||||||
|
std::cout << "Unsorted original data: " << std::endl;
|
||||||
|
show_data(data, NUM_DATA);
|
||||||
|
std::clock_t start = std::clock();
|
||||||
|
shell_sort(data, NUM_DATA);
|
||||||
|
std::clock_t end = std::clock();
|
||||||
|
|
||||||
|
std::cout << std::endl
|
||||||
|
<< "Data Sorted using custom implementation: " << std::endl;
|
||||||
|
show_data(data, NUM_DATA);
|
||||||
|
|
||||||
|
double elapsed_time = (end - start) * 1.f / CLOCKS_PER_SEC;
|
||||||
|
std::cout << "Time spent sorting: " << elapsed_time << "s\n" << std::endl;
|
||||||
|
|
||||||
|
start = std::clock();
|
||||||
|
qsort(data2, NUM_DATA, sizeof(data2[0]), compare);
|
||||||
|
end = std::clock();
|
||||||
|
std::cout << "Data Sorted using cstdlib qsort: " << std::endl;
|
||||||
|
show_data(data2, NUM_DATA);
|
||||||
|
|
||||||
|
elapsed_time = (end - start) * 1.f / CLOCKS_PER_SEC;
|
||||||
|
std::cout << "Time spent sorting: " << elapsed_time << "s\n" << std::endl;
|
||||||
|
|
||||||
|
free(data);
|
||||||
|
free(data2);
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user