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
|
5
.github/workflows/cpplint_modified_files.yml
vendored
5
.github/workflows/cpplint_modified_files.yml
vendored
@ -11,7 +11,7 @@ jobs:
|
||||
cpplint_modified_files:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1 # v2 is broken for git diff
|
||||
- uses: actions/checkout@v1 # v2 is broken for git diff
|
||||
- uses: actions/setup-python@v1
|
||||
- run: python -m pip install cpplint
|
||||
- run: git remote -v
|
||||
@ -42,7 +42,8 @@ jobs:
|
||||
print("g++:")
|
||||
# compile_exts = tuple(".c .c++ .cc .cpp .cu .cxx".split())
|
||||
# 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()]
|
||||
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
|
||||
*.out
|
||||
*.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)
|
||||
|
||||
## Computer Oriented Statistical Methods
|
||||
* [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)
|
||||
* [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)
|
||||
* [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)
|
||||
* [Gaussian Elimination](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/gaussian_elimination.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)
|
||||
* [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)
|
||||
* [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)
|
||||
|
||||
## Data Structure
|
||||
* [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)
|
||||
* [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)
|
||||
* [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)
|
||||
* [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)
|
||||
|
||||
## Others
|
||||
* [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 Hexadecimal ](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Decimal%20To%20Hexadecimal%20.cpp)
|
||||
* [Decimal To Roman Numeral](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Decimal%20to%20Roman%20Numeral.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_to_binary.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_to_roman_numeral.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)
|
||||
* [Gcd Of N Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/GCD_of_n_numbers.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)
|
||||
* [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)
|
||||
* [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)
|
||||
* [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)
|
||||
* [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)
|
||||
* [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)
|
||||
* [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%20Fibonacci.cpp)
|
||||
* [Tower Of Hanoi](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Tower%20of%20Hanoi.cpp)
|
||||
* [String Fibonacci](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/string_fibonacci.cpp)
|
||||
* [Tower Of Hanoi](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/tower_of_hanoi.cpp)
|
||||
* [Vector Important Functions](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/vector_important_functions.cpp)
|
||||
|
||||
## Probability
|
||||
@ -200,6 +199,7 @@
|
||||
* [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)
|
||||
* [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)
|
||||
* [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)
|
||||
|
@ -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 <math.h>
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
float eq(float i) {
|
||||
|
||||
static float eq(float i) {
|
||||
return (pow(i, 3) - (4 * i) - 9); // origial equation
|
||||
}
|
||||
|
||||
int main() {
|
||||
float a, b, z, c, m, n;
|
||||
system("clear");
|
||||
@ -12,12 +14,13 @@ int main() {
|
||||
if (z >= 0) {
|
||||
b = i;
|
||||
a = --i;
|
||||
goto START;
|
||||
}
|
||||
break;
|
||||
}
|
||||
START:
|
||||
}
|
||||
|
||||
std::cout << "\nFirst initial: " << a;
|
||||
std::cout << "\nSecond initial: " << b;
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
float h, d;
|
||||
m = eq(a);
|
||||
@ -26,10 +29,10 @@ int main() {
|
||||
a = c;
|
||||
z = eq(c);
|
||||
if (z > 0 && z < 0.09) { // stoping criteria
|
||||
goto END;
|
||||
break;
|
||||
}
|
||||
}
|
||||
END:
|
||||
|
||||
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,349 +1,401 @@
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* 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
|
||||
* the regression output for additional test data samples.
|
||||
**/
|
||||
*/
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* operator to print a matrix
|
||||
*/
|
||||
template <typename T>
|
||||
std::ostream &operator<<(std::ostream &out,
|
||||
std::vector<std::vector<T>> const &v) {
|
||||
const int width = 10;
|
||||
const char separator = ' ';
|
||||
const int width = 10;
|
||||
const char separator = ' ';
|
||||
|
||||
for (size_t row = 0; row < v.size(); row++) {
|
||||
for (size_t col = 0; col < v[row].size(); col++)
|
||||
out << std::left << std::setw(width) << std::setfill(separator)
|
||||
<< v[row][col];
|
||||
out << std::endl;
|
||||
}
|
||||
for (size_t row = 0; row < v.size(); row++) {
|
||||
for (size_t col = 0; col < v[row].size(); col++)
|
||||
out << std::left << std::setw(width) << std::setfill(separator)
|
||||
<< v[row][col];
|
||||
out << std::endl;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::ostream &operator<<(std::ostream &out, std::vector<T> const &v) {
|
||||
const int width = 15;
|
||||
const char separator = ' ';
|
||||
|
||||
for (size_t row = 0; row < v.size(); row++)
|
||||
out << std::left << std::setw(width) << std::setfill(separator) << v[row];
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool is_square(std::vector<std::vector<T>> const &A) {
|
||||
// Assuming A is square matrix
|
||||
size_t N = A.size();
|
||||
for (size_t i = 0; i < N; i++)
|
||||
if (A[i].size() != N)
|
||||
return false;
|
||||
return true;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* matrix multiplication
|
||||
* operator to print a vector
|
||||
*/
|
||||
template <typename T>
|
||||
std::ostream &operator<<(std::ostream &out, std::vector<T> const &v) {
|
||||
const int width = 15;
|
||||
const char separator = ' ';
|
||||
|
||||
for (size_t row = 0; row < v.size(); row++)
|
||||
out << std::left << std::setw(width) << std::setfill(separator)
|
||||
<< v[row];
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* function to check if given matrix is a square matrix
|
||||
* \returns 1 if true, 0 if false
|
||||
*/
|
||||
template <typename T>
|
||||
inline bool is_square(std::vector<std::vector<T>> const &A) {
|
||||
// Assuming A is square matrix
|
||||
size_t N = A.size();
|
||||
for (size_t i = 0; i < N; i++)
|
||||
if (A[i].size() != N) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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>
|
||||
std::vector<std::vector<T>> operator*(std::vector<std::vector<T>> const &A,
|
||||
std::vector<std::vector<T>> const &B) {
|
||||
// Number of rows in A
|
||||
size_t N_A = A.size();
|
||||
// Number of columns in B
|
||||
size_t N_B = B[0].size();
|
||||
// Number of rows in A
|
||||
size_t N_A = A.size();
|
||||
// Number of columns in B
|
||||
size_t N_B = B[0].size();
|
||||
|
||||
std::vector<std::vector<T>> result(N_A);
|
||||
std::vector<std::vector<T>> result(N_A);
|
||||
|
||||
if (A[0].size() != B.size()) {
|
||||
std::cerr << "Number of columns in A != Number of rows in B ("
|
||||
<< A[0].size() << ", " << B.size() << ")" << std::endl;
|
||||
return result;
|
||||
}
|
||||
|
||||
for (size_t row = 0; row < N_A; row++) {
|
||||
std::vector<T> v(N_B);
|
||||
for (size_t col = 0; col < N_B; col++) {
|
||||
v[col] = static_cast<T>(0);
|
||||
for (size_t j = 0; j < B.size(); j++)
|
||||
v[col] += A[row][j] * B[j][col];
|
||||
if (A[0].size() != B.size()) {
|
||||
std::cerr << "Number of columns in A != Number of rows in B ("
|
||||
<< A[0].size() << ", " << B.size() << ")" << std::endl;
|
||||
return result;
|
||||
}
|
||||
result[row] = v;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
for (size_t row = 0; row < N_A; row++) {
|
||||
std::vector<T> v(N_B);
|
||||
for (size_t col = 0; col < N_B; col++) {
|
||||
v[col] = static_cast<T>(0);
|
||||
for (size_t j = 0; j < B.size(); j++)
|
||||
v[col] += A[row][j] * B[j][col];
|
||||
}
|
||||
result[row] = v;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::vector<T> operator*(std::vector<std::vector<T>> const &A,
|
||||
std::vector<T> const &B) {
|
||||
// Number of rows in A
|
||||
size_t N_A = A.size();
|
||||
|
||||
std::vector<T> result(N_A);
|
||||
|
||||
if (A[0].size() != B.size()) {
|
||||
std::cerr << "Number of columns in A != Number of rows in B ("
|
||||
<< A[0].size() << ", " << B.size() << ")" << std::endl;
|
||||
return result;
|
||||
}
|
||||
|
||||
for (size_t row = 0; row < N_A; row++) {
|
||||
result[row] = static_cast<T>(0);
|
||||
for (size_t j = 0; j < B.size(); j++)
|
||||
result[row] += A[row][j] * B[j];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::vector<float> operator*(float const scalar, std::vector<T> const &A) {
|
||||
// Number of rows in A
|
||||
size_t N_A = A.size();
|
||||
|
||||
std::vector<float> result(N_A);
|
||||
|
||||
for (size_t row = 0; row < N_A; row++) {
|
||||
result[row] += A[row] * static_cast<float>(scalar);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::vector<float> operator*(std::vector<T> const &A, float const scalar) {
|
||||
// Number of rows in A
|
||||
size_t N_A = A.size();
|
||||
|
||||
std::vector<float> result(N_A);
|
||||
|
||||
for (size_t row = 0; row < N_A; row++)
|
||||
result[row] = A[row] * static_cast<float>(scalar);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::vector<float> operator/(std::vector<T> const &A, float const scalar) {
|
||||
return (1.f / scalar) * A;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::vector<T> operator-(std::vector<T> const &A, std::vector<T> const &B) {
|
||||
// Number of rows in A
|
||||
size_t N = A.size();
|
||||
|
||||
std::vector<T> result(N);
|
||||
|
||||
if (B.size() != N) {
|
||||
std::cerr << "Vector dimensions shouldbe identical!" << std::endl;
|
||||
return A;
|
||||
}
|
||||
|
||||
for (size_t row = 0; row < N; row++)
|
||||
result[row] = A[row] - B[row];
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::vector<T> operator+(std::vector<T> const &A, std::vector<T> const &B) {
|
||||
// Number of rows in A
|
||||
size_t N = A.size();
|
||||
|
||||
std::vector<T> result(N);
|
||||
|
||||
if (B.size() != N) {
|
||||
std::cerr << "Vector dimensions shouldbe identical!" << std::endl;
|
||||
return A;
|
||||
}
|
||||
|
||||
for (size_t row = 0; row < N; row++)
|
||||
result[row] = A[row] + B[row];
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get matrix inverse using Row-trasnformations
|
||||
* multiplication of a matrix with a column vector
|
||||
* \returns resultant vector
|
||||
*/
|
||||
template <typename T>
|
||||
std::vector<T> operator*(std::vector<std::vector<T>> const &A,
|
||||
std::vector<T> const &B) {
|
||||
// Number of rows in A
|
||||
size_t N_A = A.size();
|
||||
|
||||
std::vector<T> result(N_A);
|
||||
|
||||
if (A[0].size() != B.size()) {
|
||||
std::cerr << "Number of columns in A != Number of rows in B ("
|
||||
<< A[0].size() << ", " << B.size() << ")" << std::endl;
|
||||
return result;
|
||||
}
|
||||
|
||||
for (size_t row = 0; row < N_A; row++) {
|
||||
result[row] = static_cast<T>(0);
|
||||
for (size_t j = 0; j < B.size(); j++) result[row] += A[row][j] * B[j];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* pre-multiplication of a vector by a scalar
|
||||
* \returns resultant vector
|
||||
*/
|
||||
template <typename T>
|
||||
std::vector<float> operator*(float const scalar, std::vector<T> const &A) {
|
||||
// Number of rows in A
|
||||
size_t N_A = A.size();
|
||||
|
||||
std::vector<float> result(N_A);
|
||||
|
||||
for (size_t row = 0; row < N_A; row++) {
|
||||
result[row] += A[row] * static_cast<float>(scalar);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* post-multiplication of a vector by a scalar
|
||||
* \returns resultant vector
|
||||
*/
|
||||
template <typename T>
|
||||
std::vector<float> operator*(std::vector<T> const &A, float const scalar) {
|
||||
// Number of rows in A
|
||||
size_t N_A = A.size();
|
||||
|
||||
std::vector<float> result(N_A);
|
||||
|
||||
for (size_t row = 0; row < N_A; row++)
|
||||
result[row] = A[row] * static_cast<float>(scalar);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* division of a vector by a scalar
|
||||
* \returns resultant vector
|
||||
*/
|
||||
template <typename T>
|
||||
std::vector<float> operator/(std::vector<T> const &A, float const scalar) {
|
||||
return (1.f / scalar) * A;
|
||||
}
|
||||
|
||||
/**
|
||||
* subtraction of two vectors of identical lengths
|
||||
* \returns resultant vector
|
||||
*/
|
||||
template <typename T>
|
||||
std::vector<T> operator-(std::vector<T> const &A, std::vector<T> const &B) {
|
||||
// Number of rows in A
|
||||
size_t N = A.size();
|
||||
|
||||
std::vector<T> result(N);
|
||||
|
||||
if (B.size() != N) {
|
||||
std::cerr << "Vector dimensions shouldbe identical!" << std::endl;
|
||||
return A;
|
||||
}
|
||||
|
||||
for (size_t row = 0; row < N; row++) result[row] = A[row] - B[row];
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* addition of two vectors of identical lengths
|
||||
* \returns resultant vector
|
||||
*/
|
||||
template <typename T>
|
||||
std::vector<T> operator+(std::vector<T> const &A, std::vector<T> const &B) {
|
||||
// Number of rows in A
|
||||
size_t N = A.size();
|
||||
|
||||
std::vector<T> result(N);
|
||||
|
||||
if (B.size() != N) {
|
||||
std::cerr << "Vector dimensions shouldbe identical!" << std::endl;
|
||||
return A;
|
||||
}
|
||||
|
||||
for (size_t row = 0; row < N; row++) result[row] = A[row] + B[row];
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get matrix inverse using Row-trasnformations. Given matrix must
|
||||
* be a square and non-singular.
|
||||
* \returns inverse matrix
|
||||
**/
|
||||
template <typename T>
|
||||
std::vector<std::vector<float>>
|
||||
get_inverse(std::vector<std::vector<T>> const &A) {
|
||||
// Assuming A is square matrix
|
||||
size_t N = A.size();
|
||||
std::vector<std::vector<float>> get_inverse(
|
||||
std::vector<std::vector<T>> const &A) {
|
||||
// Assuming A is square matrix
|
||||
size_t N = A.size();
|
||||
|
||||
std::vector<std::vector<float>> inverse(N);
|
||||
for (size_t row = 0; row < N; row++) {
|
||||
// preallocatae a resultant identity matrix
|
||||
inverse[row] = std::vector<float>(N);
|
||||
for (size_t col = 0; col < N; col++)
|
||||
inverse[row][col] = (row == col) ? 1.f : 0.f;
|
||||
}
|
||||
std::vector<std::vector<float>> inverse(N);
|
||||
for (size_t row = 0; row < N; row++) {
|
||||
// preallocatae a resultant identity matrix
|
||||
inverse[row] = std::vector<float>(N);
|
||||
for (size_t col = 0; col < N; col++)
|
||||
inverse[row][col] = (row == col) ? 1.f : 0.f;
|
||||
}
|
||||
|
||||
if (!is_square(A)) {
|
||||
std::cerr << "A must be a square matrix!" << std::endl;
|
||||
return inverse;
|
||||
}
|
||||
|
||||
// preallocatae a temporary matrix identical to A
|
||||
std::vector<std::vector<float>> temp(N);
|
||||
for (size_t row = 0; row < N; row++) {
|
||||
std::vector<float> v(N);
|
||||
for (size_t col = 0; col < N; col++)
|
||||
v[col] = static_cast<float>(A[row][col]);
|
||||
temp[row] = v;
|
||||
}
|
||||
|
||||
// start transformations
|
||||
for (size_t row = 0; row < N; row++) {
|
||||
for (size_t row2 = row; row2 < N && temp[row][row] == 0; row2++) {
|
||||
// this to ensure diagonal elements are not 0
|
||||
temp[row] = temp[row] + temp[row2];
|
||||
inverse[row] = inverse[row] + inverse[row2];
|
||||
}
|
||||
|
||||
for (size_t col2 = row; col2 < N && temp[row][row] == 0; col2++) {
|
||||
// this to further ensure diagonal elements are not 0
|
||||
for (size_t row2 = 0; row2 < N; row2++) {
|
||||
temp[row2][row] = temp[row2][row] + temp[row2][col2];
|
||||
inverse[row2][row] = inverse[row2][row] + inverse[row2][col2];
|
||||
}
|
||||
}
|
||||
|
||||
if (temp[row][row] == 0) {
|
||||
// Probably a low-rank matrix and hence singular
|
||||
std::cerr << "Low-rank matrix, no inverse!" << std::endl;
|
||||
return inverse;
|
||||
}
|
||||
|
||||
// set diagonal to 1
|
||||
float divisor = static_cast<float>(temp[row][row]);
|
||||
temp[row] = temp[row] / divisor;
|
||||
inverse[row] = inverse[row] / divisor;
|
||||
// Row transformations
|
||||
for (size_t row2 = 0; row2 < N; row2++) {
|
||||
if (row2 == row) continue;
|
||||
float factor = temp[row2][row];
|
||||
temp[row2] = temp[row2] - factor * temp[row];
|
||||
inverse[row2] = inverse[row2] - factor * inverse[row];
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_square(A)) {
|
||||
std::cerr << "A must be a square matrix!" << std::endl;
|
||||
return inverse;
|
||||
}
|
||||
|
||||
// preallocatae a temporary matrix identical to A
|
||||
std::vector<std::vector<float>> temp(N);
|
||||
for (size_t row = 0; row < N; row++) {
|
||||
std::vector<float> v(N);
|
||||
for (size_t col = 0; col < N; col++)
|
||||
v[col] = static_cast<float>(A[row][col]);
|
||||
temp[row] = v;
|
||||
}
|
||||
|
||||
// start transformations
|
||||
for (size_t row = 0; row < N; row++) {
|
||||
for (size_t row2 = row; row2 < N && temp[row][row] == 0; row2++) {
|
||||
// this to ensure diagonal elements are not 0
|
||||
temp[row] = temp[row] + temp[row2];
|
||||
inverse[row] = inverse[row] + inverse[row2];
|
||||
}
|
||||
|
||||
for (size_t col2 = row; col2 < N && temp[row][row] == 0; col2++) {
|
||||
// this to further ensure diagonal elements are not 0
|
||||
for (size_t row2 = 0; row2 < N; row2++) {
|
||||
temp[row2][row] = temp[row2][row] + temp[row2][col2];
|
||||
inverse[row2][row] = inverse[row2][row] + inverse[row2][col2];
|
||||
}
|
||||
}
|
||||
|
||||
if (temp[row][row] == 0) {
|
||||
// Probably a low-rank matrix and hence singular
|
||||
std::cerr << "Low-rank matrix, no inverse!" << std::endl;
|
||||
return inverse;
|
||||
}
|
||||
|
||||
// set diagonal to 1
|
||||
float divisor = static_cast<float>(temp[row][row]);
|
||||
temp[row] = temp[row] / divisor;
|
||||
inverse[row] = inverse[row] / divisor;
|
||||
// Row transformations
|
||||
for (size_t row2 = 0; row2 < N; row2++) {
|
||||
if (row2 == row)
|
||||
continue;
|
||||
float factor = temp[row2][row];
|
||||
temp[row2] = temp[row2] - factor * temp[row];
|
||||
inverse[row2] = inverse[row2] - factor * inverse[row];
|
||||
}
|
||||
}
|
||||
|
||||
return inverse;
|
||||
}
|
||||
|
||||
/**
|
||||
* matrix transpose
|
||||
* \returns resultant matrix
|
||||
**/
|
||||
template <typename T>
|
||||
std::vector<std::vector<T>>
|
||||
get_transpose(std::vector<std::vector<T>> const &A) {
|
||||
std::vector<std::vector<T>> result(A[0].size());
|
||||
std::vector<std::vector<T>> get_transpose(
|
||||
std::vector<std::vector<T>> const &A) {
|
||||
std::vector<std::vector<T>> result(A[0].size());
|
||||
|
||||
for (size_t row = 0; row < A[0].size(); row++) {
|
||||
std::vector<T> v(A.size());
|
||||
for (size_t col = 0; col < A.size(); col++)
|
||||
v[col] = A[col][row];
|
||||
for (size_t row = 0; row < A[0].size(); row++) {
|
||||
std::vector<T> v(A.size());
|
||||
for (size_t col = 0; col < A.size(); col++) v[col] = A[col][row];
|
||||
|
||||
result[row] = v;
|
||||
}
|
||||
return result;
|
||||
result[row] = v;
|
||||
}
|
||||
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>
|
||||
std::vector<float> fit_OLS_regressor(std::vector<std::vector<T>> const &X,
|
||||
std::vector<T> const &Y) {
|
||||
// NxF
|
||||
std::vector<std::vector<T>> X2 = X;
|
||||
for (size_t i = 0; i < X2.size(); i++)
|
||||
// add Y-intercept -> Nx(F+1)
|
||||
X2[i].push_back(1);
|
||||
// (F+1)xN
|
||||
std::vector<std::vector<T>> Xt = get_transpose(X2);
|
||||
// (F+1)x(F+1)
|
||||
std::vector<std::vector<T>> tmp = get_inverse(Xt * X2);
|
||||
// (F+1)xN
|
||||
std::vector<std::vector<float>> out = tmp * Xt;
|
||||
// cout << endl
|
||||
// << "Projection matrix: " << X2 * out << endl;
|
||||
// NxF
|
||||
std::vector<std::vector<T>> X2 = X;
|
||||
for (size_t i = 0; i < X2.size(); i++)
|
||||
// add Y-intercept -> Nx(F+1)
|
||||
X2[i].push_back(1);
|
||||
// (F+1)xN
|
||||
std::vector<std::vector<T>> Xt = get_transpose(X2);
|
||||
// (F+1)x(F+1)
|
||||
std::vector<std::vector<T>> tmp = get_inverse(Xt * X2);
|
||||
// (F+1)xN
|
||||
std::vector<std::vector<float>> out = tmp * Xt;
|
||||
// cout << endl
|
||||
// << "Projection matrix: " << X2 * out << endl;
|
||||
|
||||
// Fx1,1 -> (F+1)^th element is the independent constant
|
||||
return out * Y;
|
||||
// Fx1,1 -> (F+1)^th element is the independent constant
|
||||
return out * Y;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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>
|
||||
std::vector<float> predict_OLS_regressor(std::vector<std::vector<T>> const &X,
|
||||
std::vector<float> const &beta) {
|
||||
std::vector<float> result(X.size());
|
||||
std::vector<float> const &beta /**< */
|
||||
) {
|
||||
std::vector<float> result(X.size());
|
||||
|
||||
for (size_t rows = 0; rows < X.size(); rows++) {
|
||||
// -> start with constant term
|
||||
result[rows] = beta[X[0].size()];
|
||||
for (size_t cols = 0; cols < X[0].size(); cols++)
|
||||
result[rows] += beta[cols] * X[rows][cols];
|
||||
}
|
||||
// Nx1
|
||||
return result;
|
||||
for (size_t rows = 0; rows < X.size(); rows++) {
|
||||
// -> start with constant term
|
||||
result[rows] = beta[X[0].size()];
|
||||
for (size_t cols = 0; cols < X[0].size(); cols++)
|
||||
result[rows] += beta[cols] * X[rows][cols];
|
||||
}
|
||||
// Nx1
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* main function
|
||||
*/
|
||||
int main() {
|
||||
size_t N, F;
|
||||
size_t N, F;
|
||||
|
||||
std::cout << "Enter number of features: ";
|
||||
// number of features = columns
|
||||
std::cin >> F;
|
||||
std::cout << "Enter number of samples: ";
|
||||
// number of samples = rows
|
||||
std::cin >> N;
|
||||
std::cout << "Enter number of features: ";
|
||||
// number of features = columns
|
||||
std::cin >> F;
|
||||
std::cout << "Enter number of samples: ";
|
||||
// number of samples = rows
|
||||
std::cin >> N;
|
||||
|
||||
std::vector<std::vector<float>> data(N);
|
||||
std::vector<float> Y(N);
|
||||
std::vector<std::vector<float>> data(N);
|
||||
std::vector<float> Y(N);
|
||||
|
||||
std::cout
|
||||
<< "Enter training data. Per sample, provide features ad one output."
|
||||
<< std::endl;
|
||||
std::cout
|
||||
<< "Enter training data. Per sample, provide features ad one output."
|
||||
<< std::endl;
|
||||
|
||||
for (size_t rows = 0; rows < N; rows++) {
|
||||
std::vector<float> v(F);
|
||||
std::cout << "Sample# " << rows + 1 << ": ";
|
||||
for (size_t cols = 0; cols < F; cols++)
|
||||
// get the F features
|
||||
std::cin >> v[cols];
|
||||
data[rows] = v;
|
||||
// get the corresponding output
|
||||
std::cin >> Y[rows];
|
||||
}
|
||||
for (size_t rows = 0; rows < N; rows++) {
|
||||
std::vector<float> v(F);
|
||||
std::cout << "Sample# " << rows + 1 << ": ";
|
||||
for (size_t cols = 0; cols < F; cols++)
|
||||
// get the F features
|
||||
std::cin >> v[cols];
|
||||
data[rows] = v;
|
||||
// get the corresponding output
|
||||
std::cin >> Y[rows];
|
||||
}
|
||||
|
||||
std::vector<float> beta = fit_OLS_regressor(data, Y);
|
||||
std::cout << std::endl << std::endl << "beta:" << beta << std::endl;
|
||||
std::vector<float> beta = fit_OLS_regressor(data, Y);
|
||||
std::cout << std::endl << std::endl << "beta:" << beta << std::endl;
|
||||
|
||||
size_t T;
|
||||
std::cout << "Enter number of test samples: ";
|
||||
// number of test sample inputs
|
||||
std::cin >> T;
|
||||
std::vector<std::vector<float>> data2(T);
|
||||
// vector<float> Y2(T);
|
||||
size_t T;
|
||||
std::cout << "Enter number of test samples: ";
|
||||
// number of test sample inputs
|
||||
std::cin >> T;
|
||||
std::vector<std::vector<float>> data2(T);
|
||||
// vector<float> Y2(T);
|
||||
|
||||
for (size_t rows = 0; rows < T; rows++) {
|
||||
std::cout << "Sample# " << rows + 1 << ": ";
|
||||
std::vector<float> v(F);
|
||||
for (size_t cols = 0; cols < F; cols++)
|
||||
std::cin >> v[cols];
|
||||
data2[rows] = v;
|
||||
}
|
||||
for (size_t rows = 0; rows < T; rows++) {
|
||||
std::cout << "Sample# " << rows + 1 << ": ";
|
||||
std::vector<float> v(F);
|
||||
for (size_t cols = 0; cols < F; cols++) std::cin >> v[cols];
|
||||
data2[rows] = v;
|
||||
}
|
||||
|
||||
std::vector<float> out = predict_OLS_regressor(data2, beta);
|
||||
for (size_t rows = 0; rows < T; rows++)
|
||||
std::cout << out[rows] << std::endl;
|
||||
std::vector<float> out = predict_OLS_regressor(data2, beta);
|
||||
for (size_t rows = 0; rows < T; rows++) 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 <iostream>
|
||||
|
||||
/* Calculate the the value on Fibonacci's sequence given an
|
||||
integer as input
|
||||
@ -7,14 +7,13 @@ Fibonacci = 0, 1, 1, 2, 3, 5,
|
||||
8, 13, 21, 34, 55,
|
||||
89, 144, ... */
|
||||
|
||||
int fibonacci(uint n) {
|
||||
int fibonacci(unsigned int n) {
|
||||
/* If the input is 0 or 1 just return the same
|
||||
This will set the first 2 values of the sequence */
|
||||
if (n <= 1)
|
||||
return n;
|
||||
if (n <= 1) return n;
|
||||
|
||||
/* 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);
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
@ -1,80 +1,67 @@
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#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];
|
||||
vector<int> prime_numbers;
|
||||
vector<pair<int, int>> factors;
|
||||
std::vector<int> prime_numbers;
|
||||
std::vector<std::pair<int, int>> factors;
|
||||
|
||||
// Calculating prime number upto a given range
|
||||
void SieveOfEratosthenes(int N)
|
||||
{
|
||||
void SieveOfEratosthenes(int N) {
|
||||
// initializes the array isprime
|
||||
memset(isprime, true, sizeof isprime);
|
||||
|
||||
for (int i = 2; i <= N; i++)
|
||||
{
|
||||
if (isprime[i])
|
||||
{
|
||||
for (int j = 2 * i; j <= N; j += i)
|
||||
isprime[j] = false;
|
||||
for (int i = 2; i <= N; i++) {
|
||||
if (isprime[i]) {
|
||||
for (int j = 2 * i; j <= N; j += i) isprime[j] = false;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 2; i <= N; i++)
|
||||
{
|
||||
if (isprime[i])
|
||||
prime_numbers.push_back(i);
|
||||
for (int i = 2; i <= N; i++) {
|
||||
if (isprime[i]) prime_numbers.push_back(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Prime factorization of a number
|
||||
void prime_factorization(int num)
|
||||
{
|
||||
|
||||
void prime_factorization(int 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;
|
||||
|
||||
// termination condition
|
||||
if (number == 1)
|
||||
{
|
||||
if (number == 1) {
|
||||
break;
|
||||
}
|
||||
|
||||
while (number % prime_numbers[i] == 0)
|
||||
{
|
||||
while (number % prime_numbers[i] == 0) {
|
||||
count++;
|
||||
number = number / prime_numbers[i];
|
||||
}
|
||||
|
||||
if (count)
|
||||
factors.push_back(make_pair(prime_numbers[i], count));
|
||||
if (count) factors.push_back(std::make_pair(prime_numbers[i], count));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
I added a simple UI.
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int num;
|
||||
cout << "\t\tComputes the prime factorization\n\n";
|
||||
cout << "Type in a number: ";
|
||||
cin >> num;
|
||||
std::cout << "\t\tComputes the prime factorization\n\n";
|
||||
std::cout << "Type in a number: ";
|
||||
std::cin >> num;
|
||||
|
||||
SieveOfEratosthenes(num);
|
||||
|
||||
prime_factorization(num);
|
||||
|
||||
// Prime factors with their powers in the given number in new line
|
||||
for (auto it : factors)
|
||||
{
|
||||
cout << it.first << " " << it.second << endl;
|
||||
for (auto it : factors) {
|
||||
std::cout << it.first << " " << it.second << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -1,12 +1,12 @@
|
||||
#include<iostream>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
char prime[100000000];
|
||||
|
||||
void Sieve(int64_t n) {
|
||||
memset(prime, '1', sizeof(prime)); // intitize '1' to every index
|
||||
prime[0] = '0'; // 0 is not prime
|
||||
prime[1] = '0'; // 1 is not prime
|
||||
prime[0] = '0'; // 0 is not prime
|
||||
prime[1] = '0'; // 1 is not prime
|
||||
for (int p = 2; p * p <= n; p++) {
|
||||
if (prime[p] == '1') {
|
||||
for (int i = p * p; i <= n; i += p)
|
||||
@ -15,7 +15,6 @@ void Sieve(int64_t n) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
Sieve(100000000);
|
||||
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 <vector>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
using std::vector;
|
||||
@ -46,8 +48,7 @@ vector<vector<ll>> multiply(vector<vector<ll>> A, vector<vector<ll>> B) {
|
||||
|
||||
// computing power of a matrix
|
||||
vector<vector<ll>> power(vector<vector<ll>> A, ll p) {
|
||||
if (p == 1)
|
||||
return A;
|
||||
if (p == 1) return A;
|
||||
if (p % 2 == 1) {
|
||||
return multiply(A, power(A, p - 1));
|
||||
} else {
|
||||
@ -58,14 +59,11 @@ vector<vector<ll>> power(vector<vector<ll>> A, ll p) {
|
||||
|
||||
// main function
|
||||
ll ans(ll n) {
|
||||
if (n == 0)
|
||||
return 0;
|
||||
if (n <= k)
|
||||
return b[n - 1];
|
||||
if (n == 0) return 0;
|
||||
if (n <= k) return b[n - 1];
|
||||
// F1
|
||||
vector<ll> F1(k + 1);
|
||||
for (ll i = 1; i <= k; i++)
|
||||
F1[i] = b[i - 1];
|
||||
for (ll i = 1; i <= k; i++) F1[i] = b[i - 1];
|
||||
|
||||
// Transpose matrix
|
||||
vector<vector<ll>> T(k + 1, vector<ll>(k + 1));
|
||||
|
@ -1,63 +1,53 @@
|
||||
#include<iostream>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void show_pascal(int **arr, int n)
|
||||
{
|
||||
//pint Pascal's Triangle
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
for (int j = 0; j < n + i; ++j)
|
||||
{
|
||||
if (arr[i][j] == 0)
|
||||
cout << " ";
|
||||
else
|
||||
cout << arr[i][j];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
void show_pascal(int **arr, int n) {
|
||||
// pint Pascal's Triangle
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < n + i; ++j) {
|
||||
if (arr[i][j] == 0)
|
||||
std::cout << " ";
|
||||
else
|
||||
std::cout << arr[i][j];
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
int **pascal_triangle(int **arr, int n)
|
||||
{
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
for (int j = n - i - 1; j < n + i; ++j)
|
||||
{
|
||||
if (j == n - i - 1 || j == n + i - 1)
|
||||
arr[i][j] = 1; //The edge of the Pascal triangle goes in 1
|
||||
else
|
||||
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j + 1];
|
||||
}
|
||||
}
|
||||
int **pascal_triangle(int **arr, int n) {
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = n - i - 1; j < n + i; ++j) {
|
||||
if (j == n - i - 1 || j == n + i - 1)
|
||||
arr[i][j] = 1; // The edge of the Pascal triangle goes in 1
|
||||
else
|
||||
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j + 1];
|
||||
}
|
||||
}
|
||||
|
||||
return arr;
|
||||
return arr;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int n = 0;
|
||||
int main() {
|
||||
int n = 0;
|
||||
|
||||
cout << "Set Pascal's Triangle Height" << endl;
|
||||
cin >> n;
|
||||
|
||||
//memory allocation (Assign two-dimensional array to store Pascal triangle)
|
||||
int **arr = new int*[n];
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
arr[i] = new int[2 * n - 1];
|
||||
memset(arr[i], 0, sizeof(int)*(2 * n - 1));
|
||||
}
|
||||
|
||||
pascal_triangle(arr, n);
|
||||
show_pascal(arr, n);
|
||||
std::cout << "Set Pascal's Triangle Height" << std::endl;
|
||||
std::cin >> n;
|
||||
|
||||
//deallocation
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
delete[] arr[i];
|
||||
}
|
||||
delete[] arr;
|
||||
// memory allocation (Assign two-dimensional array to store Pascal triangle)
|
||||
int **arr = new int *[n];
|
||||
for (int i = 0; i < n; ++i) {
|
||||
arr[i] = new int[2 * n - 1];
|
||||
memset(arr[i], 0, sizeof(int) * (2 * n - 1));
|
||||
}
|
||||
|
||||
return 0;
|
||||
pascal_triangle(arr, n);
|
||||
show_pascal(arr, n);
|
||||
|
||||
// deallocation
|
||||
for (int i = 0; i < n; ++i) {
|
||||
delete[] arr[i];
|
||||
}
|
||||
delete[] arr;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
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