Scientific and Engineering C++: An Introduction with Advanced Techniques and Examples

Author: John J. Barton, Lee R. Nackman
4.3
This Year Stack Overflow 1
This Month Stack Overflow 1

Comments

by anonymous   2019-07-21

Yes. It is possible. What you want to look up is "expression templates" and start from there. From that point you're going to have to build some metaprogram logic to optimize/simplify the expression. Far from a trivial task, but that's not what you asked.

NVM - it's way trivial:

int count = 0;
int modulus() { count++; return 10; }

template < typename T >
struct modular
{
  modular(T v) : value_(v) {}

  T value() const { return value_; }
  void value(T v) { value_ = v; }

  typedef modular<T> modular_type;
  typedef T raw_type;
private:
  T value_;
};

template < typename LH, typename RH >
struct multiply
{
  multiply(LH l, RH r) : lh(l), rh(r) {}

  typedef typename LH::modular_type modular_type;
  typedef typename LH::raw_type raw_type;

  raw_type value() const { return lh.value() * rh.value(); }

  operator modular_type () const { return modular_type(value() % modulus()); }

private:
  LH lh; RH rh;
};

template < typename LH, typename RH >
struct add
{
  add(LH l, RH r) : lh(l), rh(r) {}

  typedef typename LH::modular_type modular_type;
  typedef typename LH::raw_type raw_type;

  raw_type value() const { return lh.value() + rh.value(); }
  operator modular_type () const { return modular_type(value() % modulus()); }

private:
  LH lh; RH rh;
};

template < typename LH, typename RH >
add<LH,RH> operator+(LH const& lh, RH const& rh)
{
  return add<LH,RH>(lh,rh);
}

template < typename LH, typename RH >
multiply<LH,RH> operator*(LH const& lh, RH const& rh)
{
  return multiply<LH,RH>(lh,rh);
}

#include <iostream>

int main()
{
  modular<int> a = 5;
  modular<int> b = 7;
  modular<int> c = 3;
  modular<int> d = 8;

  std::cout << (5*7+3*8) % 10 << std::endl;

  modular<int> result = a * b + c * d;
  std::cout << result.value() << std::endl;

  std::cout << count << std::endl;

  std::cin.get();
}

If you were smart though, you'd put the use of % in the constructor for modular so it's always modular; you'd also put checks in to make sure LH and RH are compatible along with SFINAE crap to keep the operators from killing it for any time at all. You might also make modulus a template parameter and provide metafunctions to access it. At any rate...there you go.

Edit: BTW, you can use this same technique to make your matrices calculate faster. Instead of creating a new matrix for each operation in a string of operations, you make these things and then finally do the math, element by element, when you assign the result. There's papers on it on the internet and everything, comparing it to FORTRAN and such. Was one of the first uses of metaprogramming like template use in C++. Also in the book http://www.amazon.com/Scientific-Engineering-Introduction-Advanced-Techniques/dp/0201533936 <- keep in mind though that "advanced techniques" was in 94 :p. It's not as relevant today.

by anonymous   2018-05-29

The only computer language book I read that tries to deal seriously with concepts of Abstract algebra is Scientific and Engineering C++: An Introduction with Advanced Techniques and Examples 1st Edition by by John J. Barton, Lee R. Nackman.

It is a good book on C++, although quite old. https://www.amazon.com/Scientific-Engineering-Introduction-Advanced-Techniques/dp/0201533936/ref=sr_1_1?s=books&ie=UTF8&qid=1526872068&sr=1-1&keywords=barton+nackman

I can't tell if the approach is mathematically correct.

by litb   2018-03-19

Beginner

Introductory, no previous programming experience

* Not to be confused with C++ Primer Plus (Stephen Prata), with a significantly less favorable review.

Introductory, with previous programming experience

Best practices


Intermediate


Advanced


Reference Style - All Levels

C++11/14 References:

  • The C++ Standard (INCITS/ISO/IEC 14882-2011) This, of course, is the final arbiter of all that is or isn't C++. Be aware, however, that it is intended purely as a reference for experienced users willing to devote considerable time and effort to its understanding. As usual, the first release was quite expensive ($300+ US), but it has now been released in electronic form for $60US.

  • The C++14 standard is available, but seemingly not in an economical form – directly from the ISO it costs 198 Swiss Francs (about $200 US). For most people, the final draft before standardization is more than adequate (and free). Many will prefer an even newer draft, documenting new features that are likely to be included in C++17.

  • Overview of the New C++ (C++11/14) (PDF only) (Scott Meyers) (updated for C++1y/C++14) These are the presentation materials (slides and some lecture notes) of a three-day training course offered by Scott Meyers, who's a highly respected author on C++. Even though the list of items is short, the quality is high.

  • The C++ Core Guidelines (C++11/14/17/…) (edited by Bjarne Stroustrup and Herb Sutter) is an evolving online document consisting of a set of guidelines for using modern C++ well. The guidelines are focused on relatively higher-level issues, such as interfaces, resource management, memory management and concurrency affecting application architecture and library design. The project was announced at CppCon'15 by Bjarne Stroustrup and others and welcomes contributions from the community. Most guidelines are supplemented with a rationale and examples as well as discussions of possible tool support. Many rules are designed specifically to be automatically checkable by static analysis tools.

  • The C++ Super-FAQ (Marshall Cline, Bjarne Stroustrup and others) is an effort by the Standard C++ Foundation to unify the C++ FAQs previously maintained individually by Marshall Cline and Bjarne Stroustrup and also incorporating new contributions. The items mostly address issues at an intermediate level and are often written with a humorous tone. Not all items might be fully up to date with the latest edition of the C++ standard yet.

  • cppreference.com (C++03/11/14/17/…) (initiated by Nate Kohl) is a wiki that summarizes the basic core-language features and has extensive documentation of the C++ standard library. The documentation is very precise but is easier to read than the official standard document and provides better navigation due to its wiki nature. The project documents all versions of the C++ standard and the site allows filtering the display for a specific version. The project was presented by Nate Kohl at CppCon'14.


Classics / Older

Note: Some information contained within these books may not be up-to-date or no longer considered best practice.