Author Topic: C++ Shell  (Read 532 times)

0 Members and 1 Guest are viewing this topic.

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4756
  • Life teaches me not to want it.
    • What Now?
C++ Shell
« on: August 10, 2019, 11:02:51 am »
For future use.  We will be able to cover some examples which would assist our communications in the long run.     (in the event we live long enough):

http://cpp.sh/


a long talk I found not very pleasant but sobering

Listening to a little a this talk helped me better understand why I am studying exactly what it is I am studying today.    I am using the "Swan" book before using the "Tour" book.

I am a non-industrial mathematics hobbyist who would greatly benefit from being able to build my own stand alone C++ programs as opposed to relying only on Python and the Pythonesque computer algebra systems SageMath or SymPy (Mathematica, Maple,  MATLAB, etc).

I respect what Bjourne Stroustrup is doing. My aim is to study more math and to bring programming along with me to help me engage with the mathematics in more creative and exciting ways. 




I have little interest in industrial projects, and am solely interested in using C++ to explore mathematics and scientific computing.   I am just an aging student, but, fortunately, Professor Stroustrup is an elder Teacher.   

My brain is back to the drawing board like Wile E. Coyote trying to master the fundamentals of generic programming USING (Modern) C++.

I can't get away from this word Modern.  It was a crucial adjective used to describe the Dolciani set-theoretical presentation of the structure of "Abstract Algebra".

All it means, when it comes to C++, is using the Standard Template Libraries ... using other libraries as well, and not rebuilding ten thousand wheels.

PS:  I attached a little program (after tweaking it to query for input) to display prime factorization of integer entered.  You can open it in a text reader, select all, and paste it into the shell editor at http://cpp.sh/

Mainly for Holden, but should I die on the Garden State Parkway, I think Ibra would gladly answer basic questions you might have about compiling programs in C++

It's just a simple example of the power and novelty a little math gives to programming.

I also attached hfactor_int.cpp which uses type int rather than type "long long" as it is more direct for "learning" and readability of the code.

One day closer to death.   :D ;D

note to "self" : simplify c++ (German site?)  :  about move constructor and move assignment
« Last Edit: August 10, 2019, 05:53:32 pm by gorticide »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Share on Facebook Share on Twitter


Holden

  • { ∅, { ∅ } }
  • Posts: 5070
  • Hentrichian Philosophical Pessimist
Re: C++ Shell
« Reply #1 on: August 12, 2019, 09:10:20 am »
I have downloaded the two files. I am studying set theory at the moment.
La Tristesse Durera Toujours                                  (The Sadness Lasts Forever ...)
-van Gogh.

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4756
  • Life teaches me not to want it.
    • What Now?
Re: C++ Shell
« Reply #2 on: March 29, 2020, 12:42:00 pm »
An example of using  bindings C++17 (-std=c++17).

I will attach the code that can be compiled in the C++ shell.
It's an exercise I translated from 1988 BASIC to modern C++ 2020.

Since there is not an option at http://cpp.sh/ to choose c++17, I will upload a second version that does not use "bindings."   That is, I will simply replace  for (const auto & [f, eq] : Functions)   { with

for (const auto & t : Functions) {

and then t.first for function f, t.second for string eq.

Of course, if you have access to C++ compiler, like g++:

   g++ -g -Wall do6-5_all_table.cpp -std=c++17 -o doit_6-5_all
 
To get very cool printable copy of the code, install enscript; then:

   enscript -1rG --portrait --line-numbers -p do6-5_all_table.ps --highlight=cpp -c do6-5_all_table.cpp

A few future young and old adults may love me for doing this (after I'm dead, of course!) ...

  1. Write and run a program to approximate the slope of
     the line tangent to the graph of the function f(x) = x^3 - 4*x^2
     at x = -4, -3.5, ..., 4.
     
     Use the formula S(x) = ( f(x + h) - f(x) ) / h
     with h = 0.0001

 2. Modify and run the program in Exercise 1 for each function f
    to print a table of values in three columns.  The columns should
    include x, the calculated slope at x, and an indication of whether
    the slope is positive, negative, or zero.  Use the table to help
    determine intervals where f increases, where f decreases, and
    where f'(x) = 0.
     (a)   f(x) = (1/4)*x^3 - x
     (b)   f(x) = x^4/2 - 3*x^2
     (c)   f(x) = x^5 - 2*x^4 + x^3 + x^2 - 2*x + 1
     (d)   f(x) = -x^5 + 10*x                             
     
 3. Modify and run the program for f(x) = sqrt(x^2 + 1)   */

Just copy and paste the following (inside quote) to the C++ shell:

Quote

#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <vector>
#include <utility>  // for std::pair

double Dx(double(*f)(double x), double x0, double dx);
double f1(double x);
double f2a(double x);
double f2b(double x);
double f2c(double x);
double f2d(double x);
double f3(double x);

// PAIR:
// Store pointers to functions: double(*)(double)
// Store equations which define functions: std::string
using Function = std::pair<double(*)(double), std::string>;
                       
int main()
{
    std::vector<Function> Functions; // a vector of such pairs

    // Load up the vector with pointers to functions : FIRST
    // Load the vector with function definitions as strings : SECOND
 
    std::string eq = "f(x) = x^3 - 4*x^2";
    Function f = std::make_pair(f1, eq);
    Functions.push_back(f);
 
    eq = "f(x) = (1/4)*x^3 - x";
    f = std::make_pair(f2a, eq);
    Functions.push_back(f);

    eq = "f(x) = x^4/2 - 3*x^2";
    f = std::make_pair(f2b, eq);
    Functions.push_back(f);

    eq = "f(x) = x^5 - 2*x^4 + x^3 + x^2 - 2*x + 1";
    f = std::make_pair(f2c, eq);
    Functions.push_back(f);

    eq = "f(x) = -x^5 + 10*x";
    f = std::make_pair(f2d, eq);
    Functions.push_back(f);

    eq = "f(x) = sqrt(x^2 + 1)";
    f = std::make_pair(f3, eq);
    Functions.push_back(f);

    for (auto i : Functions)   
        std::cout << "\nFUNCTION: " << i.second << '\n';
   
    std::cout << "\n\n";
    double h = 0.0001;
    char slope;
    double tol = 0.0001;

    for (const auto & t : Functions)   {
      std::cout << "\nFunction:  " << t.second
                << "\n---------------------------------------\n"
                << "x\tS\t\tslope\n---------------------------------------\n";
      for (double j = -4; j <= 4; j += 0.5)  {
        double S = Dx(t.first, j, h);
        std::cout.precision(10);
        if (std::fabs(S) < tol) slope = '0';
        else {
          if (S > 0) slope = '+';
          if (S < 0) slope = '-';
        }
         std::cout << j << "\t" << S << "\t   " << slope << '\n';
     }
   }
   return 0;
}

double Dx(double(*f)(double x), double x0, double dx) {
   
  return  ( f(x0 + dx) - f(x0) ) / dx;

}

double f1(double x)   {
    return std::pow(x, 3) - 4*x*x;
}

double f2a(double x)  {
    return std::pow(x, 3)/4.0 - x;
}

double f2b(double x)  {
    return std::pow(x, 4)/2 - 3*x*x;
}

double f2c(double x)  {
     return std::pow(x, 5) - 2*std::pow(x, 4) + std::pow(x, 3) + x*x - 2*x + 1;
}

double f2d(double x)  {
    return -std::pow(x, 5) + 10*x;
}

double f3(double x)  {
    return std::sqrt(x*x + 1);
}

/*
   NOTES:

  double(*f)(double x)
  The above tells the compiler that the parameter f is a pointer to a function.
  The pointed-to function takes a double input and returns a double.

   bindings C++17 (-std=c++17): for (const auto & [f, eq] : Functions)
   AND std::pow from <cmath>
   
   g++ -g -Wall do6-5_all_table.cpp -std=c++17 -o doit_6-5_all
 
   enscript -1rG --portrait --line-numbers -p do6-5_all_table.ps
               --highlight=cpp -c do6-5_all_table.cpp

if using do6-5_not++17.cpp (the code insode the above quote) and not pasting into http://cpp.sh/ :
g++ -g -Wall do6-5_not++17.cpp -o doit_6-5_all
 */
« Last Edit: March 29, 2020, 01:23:28 pm by mike »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4756
  • Life teaches me not to want it.
    • What Now?
Re: C++ Shell
« Reply #3 on: March 29, 2020, 03:34:13 pm »
I figured out how you can inspect code and output without a compiler on your local machine: [hit "run" and check under "execution" tab] ---->   http://cpp.sh/3blim

A giant step for us, I think.   Maybe this will give you a glimpse of what the code is doing.  Note that it is more "fun" stepping through it with gdb debugger.

My stomach growls, and I wish I could ignore it.  Man cannot live on math and code alone!

Peanut butter monstrosity.

The output from the code then serves as a guide to describe the intervals where the slope is rising or falling or no slope (horizontal), possible relative extremum.  I have uploaded the written part of the solutions that go with the code to The Wayback Machine in a zip file (3 PDF files).
« Last Edit: March 30, 2020, 02:06:00 am by mike »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~