Last active
January 7, 2018 12:43
-
-
Save ghabxph/a308979389885bee80b3bff23372fee7 to your computer and use it in GitHub Desktop.
Sigmoid Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright 2017 ghabxph(c) | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is furnished | |
* to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
#define _WIN32_WINNT 0x0500 | |
#include <windows.h> | |
#include <iostream> | |
#include <cmath> | |
#include <string> | |
using namespace std; | |
/** | |
* Calculates the sigmoid of min to max, with a given number of iterations. | |
* @author ghabxph | |
* @since 2018.1.5 | |
*/ | |
/// ------------------------ | |
/// Prototypes | |
/// ```````````````````````` | |
/// Sigmoid function | |
double sigmoid(double value); | |
/// Retrieves valid integer input from input stream | |
double getDoubleInput (const char *sMessage); | |
/// Retrieves valid integer input from input stream | |
int getIntInput (const char *sMessage); | |
/// Plots something in the cartesian plane | |
void plot (HDC hDeviceContext, double x, double y, COLORREF color, double xUnitPerDot, double yUnitPerDot); | |
/// ------------------------ | |
/// Definitions / Macros | |
#define CONSOLE_WIDTH 959 | |
#define CONSOLE_HEIGHT 418 | |
/// ------------------------ | |
/// Main Application | |
/// ```````````````````````` | |
int main() | |
{ | |
double minValue = getDoubleInput("Enter min value: "); | |
double maxValue = getDoubleInput("Enter max value: "); | |
int iterations = getIntInput("Enter number of iterations: "); | |
double zoomX = getDoubleInput("Enter ZoomX: "); | |
double zoomY = getDoubleInput("Enter ZoomY: "); | |
int actualIterations = 0; | |
double step = abs(maxValue - minValue) / iterations; | |
double result = minValue; | |
// cout << "sigmoid(" << minValue << ")\t: " << sigmoid(result) << endl; | |
// for (int i = 0; i < iterations; i++) { | |
// result += step; | |
// actualIterations++; | |
// cout << "sigmoid(" << result << ")\t: " << sigmoid(result) << endl; | |
// } | |
// return 0; | |
/// Get console handle | |
HWND hConsole = GetConsoleWindow(); | |
/// Get a handle to device context | |
HDC hDeviceContext = GetDC(hConsole); | |
COLORREF XEE = RGB(0xEE, 0xEE, 0xEE); | |
COLORREF X66 = RGB(0x66, 0x66, 0x66); | |
COLORREF X33 = RGB(0x33, 0x33, 0x33); | |
COLORREF X00FF00 = RGB(0x00, 0xFF, 0x00); | |
while (true) { | |
for (int i = 0; i < CONSOLE_WIDTH; i++) { | |
SetPixel(hDeviceContext, i, CONSOLE_HEIGHT / 2, X33); | |
} | |
for (int i = 0; i < CONSOLE_HEIGHT; i++) { | |
SetPixel(hDeviceContext, CONSOLE_WIDTH / 2, i, X33); | |
} | |
result = minValue; | |
for (int i = 0; i < iterations; i++) { | |
result += step; | |
actualIterations++; | |
plot(hDeviceContext, result, sigmoid(result), XEE, CONSOLE_WIDTH / zoomX, CONSOLE_HEIGHT / zoomY); | |
} | |
} | |
ReleaseDC(hConsole, hDeviceContext); | |
cin.ignore(); | |
return 0; | |
} | |
/// ------------------------ | |
/// ------------------------ | |
/// Implementations | |
/// ```````````````````````` | |
/// Sigmoid function | |
double sigmoid(double value) { | |
return 1 / (1 + exp(-value)); | |
} | |
/// Retrieves valid double input from input stream | |
double getDoubleInput (const char *sMessage) | |
{ | |
bool bInputGood = false; | |
double inputVar; | |
do { | |
cout << sMessage; | |
cin >> inputVar; | |
bInputGood = cin.good(); | |
if (bInputGood == false) { | |
cout << "Bad input. Please try again." << endl; | |
} | |
cin.clear(); | |
cin.ignore(INT_MAX, '\n'); | |
} while (bInputGood == false); | |
return inputVar; | |
} | |
/// Retrieves valid double input from input stream | |
int getIntInput (const char *sMessage) | |
{ | |
bool bInputGood = false; | |
int inputVar; | |
do { | |
cout << sMessage; | |
cin >> inputVar; | |
bInputGood = cin.good(); | |
if (bInputGood == false) { | |
cout << "Bad input. Please try again." << endl; | |
} | |
cin.clear(); | |
cin.ignore(INT_MAX, '\n'); | |
} while (bInputGood == false); | |
return inputVar; | |
} | |
/// Plots something in the cartesian plane | |
void plot (HDC hDeviceContext, double x, double y, COLORREF color, double xUnitPerDot, double yUnitPerDot) | |
{ | |
int xUnit = (int)(x * 4 * xUnitPerDot); | |
int yUnit = (int)(y * 4 * yUnitPerDot); | |
SetPixel(hDeviceContext, (CONSOLE_WIDTH / 2) + xUnit - 1, (CONSOLE_HEIGHT / 2) - yUnit + 1, color); | |
SetPixel(hDeviceContext, (CONSOLE_WIDTH / 2) + xUnit + 0, (CONSOLE_HEIGHT / 2) - yUnit + 1, color); | |
SetPixel(hDeviceContext, (CONSOLE_WIDTH / 2) + xUnit + 1, (CONSOLE_HEIGHT / 2) - yUnit + 1, color); | |
SetPixel(hDeviceContext, (CONSOLE_WIDTH / 2) + xUnit - 1, (CONSOLE_HEIGHT / 2) - yUnit + 0, color); | |
SetPixel(hDeviceContext, (CONSOLE_WIDTH / 2) + xUnit + 0, (CONSOLE_HEIGHT / 2) - yUnit + 0, color); | |
SetPixel(hDeviceContext, (CONSOLE_WIDTH / 2) + xUnit + 1, (CONSOLE_HEIGHT / 2) - yUnit + 0, color); | |
SetPixel(hDeviceContext, (CONSOLE_WIDTH / 2) + xUnit - 1, (CONSOLE_HEIGHT / 2) - yUnit + 1, color); | |
SetPixel(hDeviceContext, (CONSOLE_WIDTH / 2) + xUnit + 0, (CONSOLE_HEIGHT / 2) - yUnit + 1, color); | |
SetPixel(hDeviceContext, (CONSOLE_WIDTH / 2) + xUnit + 1, (CONSOLE_HEIGHT / 2) - yUnit + 1, color); | |
} | |
/// ------------------------ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment