Tải bản đầy đủ (.pdf) (521 trang)

c++ for mathematicians - an introduction for students and professionals (2006)

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (30.18 MB, 521 trang )

C++ for Mathematicians
An Introduction for Students and Professionals
Edward Scheinerman

Cover photograph:

Ira Scheinerman

Cover design concept:

Jonah Scheinerman
Published in 2006 by
Chapman & Hall/CRC
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
© 2006 by Taylor & Francis Group, LLC
Chapman & Hall/CRC is an imprint of Taylor & Francis Group
No claim to original U.S. Government works
Printed in the United States of America on acid-free paper
10987654321
International Standard Book Number-10: 1-58488-584-X (Softcover)
International Standard Book Number-13: 978-0978-1-58488-584-9 (Softcover)
This book contains information obtained from authentic and highly regarded sources. Reprinted material is
quoted with permission, and sources are indicated. A wide variety of references are listed. Reasonable efforts
have been made to publish reliable data and information, but the author and the publisher cannot assume
responsibility for the validity of all materials or for the consequences of their use.
No part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic,
mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and
recording, or in any information storage or retrieval system, without written permission from the publishers.


For permission to photocopy or use material electronically from this work, please access www.copyright.com
( or contact the Copyright Clearance Center, Inc. (CCC) 222 Rosewood Drive,
Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and registration
for a variety of users. For organizations that have been granted a photocopy license by the CCC, a separate
system of payment has been arranged.

Trademark Notice:

Product or corporate names may be trademarks or registered trademarks, and are used only
for identification and explanation without intent to infringe.

Visit the Taylor & Francis Web site at

and the CRC Press Web site at

Taylor & Francis Group
is the Academic Division of Informa plc.

C584X_Discl Page 1 Tuesday, April 18, 2006 1:58 PM
In loving memory of Pauline and of Arnold

Contents
List of Programs xiii
List of Figures xvii
Preface xix
I Procedures 1
1 The Basics 3
1.1 What is C++? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Hello C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2 Numbers 11
2.1 The integer types . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.2 The real number types . . . . . . . . . . . . . . . . . . . . . . . 14
2.3 The bool and char types . . . . . . . . . . . . . . . . . . . . . 14
2.4 Checking the size and capacity of the different types . . . . . . . 15
2.5 Standard operations . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.6 Comparisons and Boolean operations . . . . . . . . . . . . . . . 22
2.7 Complex numbers . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.8 Naming variables . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3 Greatest Common Divisor 31
3.1 The problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.2 A first approach . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.3 Euclid’s method . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.4 Looping with for, while, and do . . . . . . . . . . . . . . . . 41
3.5 An exhaustive approach to the GCD problem . . . . . . . . . . . 43
3.6 Extended gcd, call by reference, and overloading . . . . . . . . . 45
3.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
4 Random Numbers 53
4.1 Pseudo random number generation . . . . . . . . . . . . . . . . . 53
4.2 Uniform random values . . . . . . . . . . . . . . . . . . . . . . . 54
4.3 More on pseudo random number generation . . . . . . . . . . . . 57
4.4 A Monte Carlo program for the GCD problem . . . . . . . . . . . 60
v
vi C++ for Mathematicians
4.5 Normal random values . . . . . . . . . . . . . . . . . . . . . . . 61
4.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
5 Arrays 67
5.1 Euler’s totient . . . . . . . . . . . . . . . . . . . . . . . . . . . .
67

5.2 Array fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . 69
5.3 A procedure to factor integers . . . . . . . . . . . . . . . . . . . 71
5.4 A procedure to calculate Euler’s totient . . . . . . . . . . . . . . . 76
5.5 The Sieve of Eratosthenes: new and delete[] . . . . . . . . . 78
5.6 A faster totient . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
5.7 Computing p
n
for large n . . . . . . . . . . . . . . . . . . . . . . 85
5.8 The answer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
5.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
II Objects 91
6 Points in the Plane 93
6.1 Data and methods . . . . . . . . . . . . . . . . . . . . . . . . . . 93
6.2 Declaring the Point class . . . . . . . . . . . . . . . . . . . . . 94
6.3 Data hiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
6.4 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
6.5 Assignment and conversion . . . . . . . . . . . . . . . . . . . . . 100
6.6 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
6.7 Procedures using arguments of type Point . . . . . . . . . . . . 103
6.8 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
6.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
7 Pythagorean Triples 115
7.1 Generating Pythagorean triples . . . . . . . . . . . . . . . . . . . 115
7.2 Designing a primitive Pythagorean triple class . . . . . . . . . . . 116
7.3 Implementation of the PTriple class . . . . . . . . . . . . . . . 117
7.4 Finding and sorting the triples . . . . . . . . . . . . . . . . . . . 121
7.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
8 Containers 127
8.1 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
8.2 Set iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130

8.3 Multisets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
8.4 Adjustable arrays via the vector class . . . . . . . . . . . . . . 134
8.5 Ordered pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
8.6 Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
8.7 Lists, stacks, and assorted queues . . . . . . . . . . . . . . . . . . 144
8.7.1 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
8.7.2 Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
8.7.3 Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Table of Contents vii
8.7.4 Deques . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
8.7.5 Priority queues . . . . . . . . . . . . . . . . . . . . . . . 150
8.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
9 Modular Arithmetic 157
9.1 Designing the Mod type . . . . . . . . . . . . . . . . . . . . . . . 157
9.2 The code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
9.3 The default modulus: Static class variables and methods . . . . . 163
9.4 Constructors and get/set methods . . . . . . . . . . . . . . . . . . 167
9.5 Comparison operators . . . . . . . . . . . . . . . . . . . . . . . . 167
9.6 Arithmetic operators . . . . . . . . . . . . . . . . . . . . . . . . 169
9.7 Writing Mod objects to output streams . . . . . . . . . . . . . . . 172
9.8 A main to demonstrate the Mod class . . . . . . . . . . . . . . . 172
9.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
10 The Projective Plane 177
10.1 Introduction to the projective plane, RP
2
. . . . . . . . . . . . . . 177
10.2 Designing the classes PPoint and PLine . . . . . . . . . . . . 178
10.3 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
10.4 Protected class members . . . . . . . . . . . . . . . . . . . . . . 184
10.5 Class and file organization for PPoint and PLine . . . . . . . . 186

10.6 The parent class PObject . . . . . . . . . . . . . . . . . . . . . 187
10.7 The classes PPoint and PLine . . . . . . . . . . . . . . . . . . 195
10.8 Discovering and repairing a bug . . . . . . . . . . . . . . . . . . 200
10.9 Pappus revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
10.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
11 Permutations 215
11.1 Ulam’s problem . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
11.2 Designing the Permutation class . . . . . . . . . . . . . . . . 217
11.2.1 Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
11.2.2 Constructors and destructors . . . . . . . . . . . . . . . . 218
11.2.3 Copy and assign . . . . . . . . . . . . . . . . . . . . . . . 220
11.2.4 Basic inspection and modification methods . . . . . . . . . 223
11.2.5 Permutation operations . . . . . . . . . . . . . . . . . . . 224
11.2.6 Comparison operators . . . . . . . . . . . . . . . . . . . . 225
11.2.7 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
11.2.8 The code file Permutation.c . . . . . . . . . . . . . . 225
11.3 Finding monotone subsequences . . . . . . . . . . . . . . . . . . 229
11.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
viii C++ for Mathematicians
12 Polynomials 235
12.1 Procedure templates . . . . . . . . . . . . . . . . . . . . . . . . . 235
12.2 Class templates . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
12.2.1 Using class templates . . . . . . . . . . . . . . . . . . . . 238
12.2.2 Creating class templates . . . . . . . . . . . . . . . . . . . 239
12.3 The Polynomial class template . . . . . . . . . . . . . . . . . 242
12.3.1 Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
12.3.2 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . 243
12.3.3 Get and set methods . . . . . . . . . . . . . . . . . . . . . 244
12.3.4 Function methods . . . . . . . . . . . . . . . . . . . . . . 245
12.3.5 Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . 246

12.3.6 Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . 246
12.3.7 Output to the screen . . . . . . . . . . . . . . . . . . . . . 247
12.3.8 GCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
12.3.9 The code . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
12.4 The GCD problem revisited . . . . . . . . . . . . . . . . . . . . . 254
12.5 Working in binary . . . . . . . . . . . . . . . . . . . . . . . . . . 258
12.5.1 Signed versus unsigned integers . . . . . . . . . . . . . . 258
12.5.2 Bit operations . . . . . . . . . . . . . . . . . . . . . . . . 259
12.5.3 The bitset class template . . . . . . . . . . . . . . . . 260
12.5.4 Class templates with non-type arguments . . . . . . . . . . 263
12.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
III Topics 267
13 Using Other Packages 269
13.1 Arbitrary precision arithmetic: The GMP package . . . . . . . . . 269
13.2 Linear algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
13.2.1 Two-dimensional arrays in C++ . . . . . . . . . . . . . . . 273
13.2.2 The TNT and JAMA packages . . . . . . . . . . . . . . . 274
13.2.3 The newmat package . . . . . . . . . . . . . . . . . . . . 282
13.3 Other packages . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
13.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
14 Strings, Input/Output, and Visualization 289
14.1 Character arrays . . . . . . . . . . . . . . . . . . . . . . . . . . .
289
14.2 The string class . . . . . . . . . . . . . . . . . . . . . . . . . 291
14.2.1 Initialization . . . . . . . . . . . . . . . . . . . . . . . . . 291
14.2.2 Fundamental operations . . . . . . . . . . . . . . . . . . . 292
14.2.3 Searching . . . . . . . . . . . . . . . . . . . . . . . . . . 295
14.2.4 Converting between string and char
*
types . . . . . . 297

14.3 Command line arguments . . . . . . . . . . . . . . . . . . . . . . 297
14.4 Reading and writing data in files . . . . . . . . . . . . . . . . . . 300
14.4.1 Opening files for input/output . . . . . . . . . . . . . . . . 300
14.4.2 Reading and writing . . . . . . . . . . . . . . . . . . . . . 303
Table of Contents ix
14.4.3 Detecting the end of an input file . . . . . . . . . . . . . . 304
14.4.4 Other methods for input . . . . . . . . . . . . . . . . . . . 305
14.5 String streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
14.6 Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
14.6.1 Setting precision . . . . . . . . . . . . . . . . . . . . . . 309
14.6.2 Showing all digits . . . . . . . . . . . . . . . . . . . . . . 309
14.6.3 Setting the width . . . . . . . . . . . . . . . . . . . . . . 310
14.6.4 Other manipulators . . . . . . . . . . . . . . . . . . . . . 311
14.7 A class to parse files . . . . . . . . . . . . . . . . . . . . . . . . 311
14.8 Visualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
14.8.1 Introducing and installing the plotutils package . . . . 316
14.8.2 Drawing with plotutils—a first example . . . . . . . 317
14.8.3 Pascal’s triangle modulo two . . . . . . . . . . . . . . . . 322
14.8.4 Tracing the motion of a point moving randomly in a triangle 324
14.8.5 Drawing Paley graphs . . . . . . . . . . . . . . . . . . . . 326
14.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
15 Odds and Ends 333
15.1 The switch statement . . . . . . . . . . . . . . . . . . . . . . . 333
15.2 Labels and the goto statement . . . . . . . . . . . . . . . . . . . 336
15.3 Exception handling . . . . . . . . . . . . . . . . . . . . . . . . . 338
15.3.1 The basics of try, throw, and catch . . . . . . . . . . 338
15.3.2 Other features of the exception-handling system . . . . . . 342
15.4 Friends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
15.5 Other ways to create types . . . . . . . . . . . . . . . . . . . . . 347
15.5.1 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . 347

15.5.2 Enumerations . . . . . . . . . . . . . . . . . . . . . . . . 348
15.5.3 Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
15.5.4 Using typedef . . . . . . . . . . . . . . . . . . . . . . 349
15.6 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
15.6.1 Pointer basics . . . . . . . . . . . . . . . . . . . . . . . . 350
15.6.2 Dereferencing . . . . . . . . . . . . . . . . . . . . . . . . 351
15.6.3 Arrays and pointer arithmetic . . . . . . . . . . . . . . . . 353
15.6.4 new and delete revisited . . . . . . . . . . . . . . . . . 355
15.6.5 Why use pointers? . . . . . . . . . . . . . . . . . . . . . . 356
15.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
IV Appendices 361
A Your C++ Computing Environment 363
A.1 Programming with a command window and a text editor . . . . . 363
A.1.1 What you need and how to get it (for free) . . . . . . . . . 364
A.1.2 Editing program files . . . . . . . . . . . . . . . . . . . . 365
A.1.3 Compiling and running your program . . . . . . . . . . . 366
A.1.4 Compiler options . . . . . . . . . . . . . . . . . . . . . . 368
x C++ for Mathematicians
A.1.5 Introduction to make . . . . . . . . . . . . . . . . . . . . 370
A.2 Programming with an integrated development environment . . . . 372
A.2.1 Visual C++ for Windows . . . . . . . . . . . . . . . . . . 373
A.2.2 Xcode for Macintosh OS X . . . . . . . . . . . . . . . . . 376
A.3 General advice on debugging . . . . . . . . . . . . . . . . . . . . 378
B Documentation with Doxygen 381
B.1 Doxygen comments . . . . . . . . . . . . . . . . . . . . . . . . . 381
B.1.1 Documenting files . . . . . . . . . . . . . . . . . . . . . . 382
B.1.2 Documenting procedures . . . . . . . . . . . . . . . . . . 382
B.1.3 Documenting classes, data, and methods . . . . . . . . . . 383
B.2 Using Doxygen . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
B.2.1 Configuring Doxygen . . . . . . . . . . . . . . . . . . . . 386

B.2.2 Running Doxygen . . . . . . . . . . . . . . . . . . . . . . 389
B.2.3 More features . . . . . . . . . . . . . . . . . . . . . . . . 389
C C++ Reference 391
C.1 Variables and types . . . . . . . . . . . . . . . . . . . . . . . . . 391
C.1.1 Fundamental types . . . . . . . . . . . . . . . . . . . . . 391
C.1.2 Standard classes/templates . . . . . . . . . . . . . . . . . 391
C.1.3 Declaring variables . . . . . . . . . . . . . . . . . . . . . 392
C.1.4 Static variables and scope . . . . . . . . . . . . . . . . . . 392
C.1.5 Constants and the keyword const . . . . . . . . . . . . . 393
C.1.6 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
C.2 Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
C.2.1 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . 394
C.2.2 Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . 394
C.2.3 Comparison operators . . . . . . . . . . . . . . . . . . . . 394
C.2.4 Logical operators . . . . . . . . . . . . . . . . . . . . . . 394
C.2.5 Bit operators . . . . . . . . . . . . . . . . . . . . . . . . . 395
C.2.6 Potpourri . . . . . . . . . . . . . . . . . . . . . . . . . . 395
C.3 Control statements . . . . . . . . . . . . . . . . . . . . . . . . . 396
C.3.1 if-else . . . . . . . . . . . . . . . . . . . . . . . . . . 396
C.3.2 Looping: for, while, and do . . . . . . . . . . . . . . . 396
C.3.3 switch . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
C.3.4 goto . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
C.3.5 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . 398
C.4 Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
C.4.1 File organization . . . . . . . . . . . . . . . . . . . . . . 399
C.4.2 Call by value versus call by reference . . . . . . . . . . . 399
C.4.3 Array (and pointer) arguments . . . . . . . . . . . . . . . 400
C.4.4 Default values for arguments . . . . . . . . . . . . . . . . 400
C.4.5 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . 400
C.4.6 inline procedures . . . . . . . . . . . . . . . . . . . . . 401

C.5 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Table of Contents xi
C.5.1 Overview and file organization . . . . . . . . . . . . . . . 401
C.5.2 Constructors and destructors . . . . . . . . . . . . . . . . 402
C.5.3 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . 403
C.5.4 Copy and assign . . . . . . . . . . . . . . . . . . . . . . . 404
C.5.5 static data and methods . . . . . . . . . . . . . . . . . 405
C.5.6 this . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
C.5.7 Friends . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
C.5.8 Class templates . . . . . . . . . . . . . . . . . . . . . . . 407
C.5.9 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . 407
C.6 Standard functions . . . . . . . . . . . . . . . . . . . . . . . . . 408
C.6.1 Mathematical functions . . . . . . . . . . . . . . . . . . . 408
C.6.2 Mathematical constants . . . . . . . . . . . . . . . . . . . 411
C.6.3 Character procedures . . . . . . . . . . . . . . . . . . . . 411
C.6.4 Other useful functions . . . . . . . . . . . . . . . . . . . . 413
D Answers 415
Index 487

Programs
1.1 Poem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1 Introducing the int type. . . . . . . . . . . . . . . . . . . . . . 11
2.2 A program to illustrate integer overflow. . . . . . . . . . . . . . . 13
2.3 A program to show the sizes of the fundamental data types. . . . . 15
2.4 Extreme values of various data types. . . . . . . . . . . . . . . . 17
2.5 A program to explore C++’s mod operation. . . . . . . . . . . . . 19
2.6 A program to calculate e
π
and π
e

. . . . . . . . . . . . . . . . . . 21
2.7 Handling complex numbers. . . . . . . . . . . . . . . . . . . . . 23
2.8 A header file, complexx.h. . . . . . . . . . . . . . . . . . . . 24
3.1 The header file gcd.h. . . . . . . . . . . . . . . . . . . . . . . . 32
3.2 Revised documentation for gcd in the header file gcd.h. . . . . 34
3.3 Beginning of the file gcd.cc. . . . . . . . . . . . . . . . . . . . 34
3.4 Ensuring a and b are nonnegative in gcd.cc. . . . . . . . . . . 35
3.5 The last part of the gcd.cc program. . . . . . . . . . . . . . . . 35
3.6 A program to test the gcd procedure. . . . . . . . . . . . . . . . 37
3.7 A recursive procedure for gcd. . . . . . . . . . . . . . . . . . . 38
3.8 An iterative procedure for gcd. . . . . . . . . . . . . . . . . . . 40
3.9 A program to calculate p
n
. . . . . . . . . . . . . . . . . . . . . . 43
3.10 A slightly better program to calculate p
n
. . . . . . . . . . . . . . 44
3.11 Code for the extended gcd procedure. . . . . . . . . . . . . . . . 48
4.1 Header file uniform.h. . . . . . . . . . . . . . . . . . . . . . 54
4.2 Definitions of the unif procedures in uniform.cc. . . . . . . 56
4.3 The problem with lower-order bits in an LCG. . . . . . . . . . . . 58
4.4 A Monte Carlo approach to calculating p
n
. . . . . . . . . . . . . 60
4.5 A program to generate Gaussian random values. . . . . . . . . . . 63
5.1 Header file for first version of factor. . . . . . . . . . . . . . . 73
5.2 Source file for first version of factor. . . . . . . . . . . . . . . 74
5.3 A main to test the factor procedure. . . . . . . . . . . . . . . 75
5.4 Header file for the totient procedure. . . . . . . . . . . . . . . 76
5.5 The code for the totient procedure. . . . . . . . . . . . . . . . 77

5.6 The header file sieve.h. . . . . . . . . . . . . . . . . . . . . . 79
5.7 The sieve procedure. . . . . . . . . . . . . . . . . . . . . . . . 81
5.8 A program to test the sieve procedure. . . . . . . . . . . . . . . 82
5.9 A faster totient procedure that employs a table of primes. . . . 84
5.10 A program to calculate p
n
for n equal to one million. . . . . . . . 85
6.1 Header file Point.h for the Point class (condensed version). . 95
6.2 Code for the Point class methods and procedures. . . . . . . . . 109
xiii
xiv C++ for Mathematicians
6.3 A program to check the Point class. . . . . . . . . . . . . . . . 110
7.1 Header file for the PTriple class. . . . . . . . . . . . . . . . . 117
7.2 Program file for the PTriple class. . . . . . . . . . . . . . . . . 120
7.3 A program to find Pythagorean triples. . . . . . . . . . . . . . . . 122
8.1 A program to find Pythagorean triples using sets. . . . . . . . . . 129
8.2 A program to demonstrate the use of multiset. . . . . . . . . . 133
8.3 The Sieve of Eratosthenes revisiting using vector classes. . . . 137
8.4 A program to illustrate the use of maps. . . . . . . . . . . . . . . 141
8.5 A procedure that remembers values it has already calculated. . . . 143
8.6 A program to demonstrate the use of lists. . . . . . . . . . . . 146
8.7 A program to illustrate the deque container. . . . . . . . . . . . 150
8.8 Demonstrating the use of the priority queue container. . . . 151
9.1 Header file for the Mod class, Mod.h. . . . . . . . . . . . . . . . 159
9.2 Source file for the Mod class, Mod.cc. . . . . . . . . . . . . . . 162
9.3 A program to illustrate the use of the Mod class. . . . . . . . . . . 172
10.1 A program to illustrate inheritance. . . . . . . . . . . . . . . . . 181
10.2 Using protected members of a class. . . . . . . . . . . . . . . 184
10.3 Header file for all projective geometry classes, Projective.h. 186
10.4 Header file for the PObject class (version 1). . . . . . . . . . . 191

10.5 Program file for the PObject class (version 1). . . . . . . . . . 192
10.6 Header file for the PPoint class. . . . . . . . . . . . . . . . . . 197
10.7 Program file for the PPoint class. . . . . . . . . . . . . . . . . 198
10.8 Header file for the PLine class. . . . . . . . . . . . . . . . . . . 198
10.9 Program file for the PLine class. . . . . . . . . . . . . . . . . . 199
10.10 A main to test the RP
2
classes. . . . . . . . . . . . . . . . . . . 200
10.11 Header file for the PObject class (version 2). . . . . . . . . . . 202
10.12 Program file for the PObject class (version 2). . . . . . . . . . 203
10.13 A program to illustrate Pappus’s theorem and its dual. . . . . . . 207
11.1 Header file for Permutation class, Permutation.h. . . . . 217
11.2 Program file for Permutation class. . . . . . . . . . . . . . . 226
11.3 Header file monotone.h. . . . . . . . . . . . . . . . . . . . . . 230
11.4 Finding longest monotone subsequences. . . . . . . . . . . . . . 230
11.5 A program to illustrate Ulam’s problem. . . . . . . . . . . . . . . 231
12.1 Header file for the max of three template. . . . . . . . . . . . 236
12.2 The template for the mycomplex classes. . . . . . . . . . . . . 240
12.3 Revised version of mycomplex. . . . . . . . . . . . . . . . . . 241
12.4 Header file for the Polynomial class template. . . . . . . . . . 247
12.5 Header file long2poly.h. . . . . . . . . . . . . . . . . . . . . 255
12.6 Code file for the long2poly procedure. . . . . . . . . . . . . . 256
12.7 Main program for the GCD revisited problem. . . . . . . . . . . . 256
13.1 A program to illustrate the use of the GMP package. . . . . . . . 271
13.2 Assignment versus copying in the TNT package. . . . . . . . . . 275
13.3 A template to calculate the trace of an Array2D matrix. . . . . . 277
13.4 Using TNT and JAMA on a Hilbert matrix. . . . . . . . . . . . . 280
13.5 Using newmat on a Hilbert matrix. . . . . . . . . . . . . . . . . 285
Programs xv
14.1 A program to illustrate the sorting of string values. . . . . . . 294

14.2 Accessing command line arguments. . . . . . . . . . . . . . . . . 298
14.3 Calculating the gcd of command line arguments. . . . . . . . . . 299
14.4 A program the processes files specified on the command line. . . 302
14.5 A program that illustrates writing data to a file. . . . . . . . . . . 303
14.6 A program that sums the integer values it finds in a file. . . . . . . 304
14.7 A program to illustrate the use of string streams. . . . . . . . . . 308
14.8 Header file for the LineParser class. . . . . . . . . . . . . . . 313
14.9 Program file for the LineParser class. . . . . . . . . . . . . . 313
14.10 A program to demonstrate the use of the LineParser class. . . 314
14.11 A program to draw the symbol ⊗. . . . . . . . . . . . . . . . . . 321
14.12 Visualizing Pascal’s triangle mod 2. . . . . . . . . . . . . . . . . 322
14.13 A program to plot points in a triangle by a random method. . . . . 325
14.14 A program to draw Paley graphs. . . . . . . . . . . . . . . . . . . 328
15.1 A program to illustrate the switch statement. . . . . . . . . . . 334
15.2 Basic exception handling. . . . . . . . . . . . . . . . . . . . . . 339
15.3 Catching exceptions thrown by other procedures. . . . . . . . . . 340
15.4 A new Point.h header with friend procedures. . . . . . . . . 345
15.5 Illustrating pointer dereferencing. . . . . . . . . . . . . . . . . . 351
A.1 A basic Makefile. . . . . . . . . . . . . . . . . . . . . . . . . 370
B.1 Documenting a procedure for Doxygen. . . . . . . . . . . . . . . 382
B.2 Documenting a class and its members for Doxygen. . . . . . . . . 384

Figures
1.1 PDP-8 front panel switches. . . . . . . . . . . . . . . . . . . . . . 3
5.1 A flowchart for the factoring algorithm. . . . . . . . . . . . . . . . 73
5.2 Illustrating the Sieve of Eratosthenes algorithm. . . . . . . . . . . . 79
10.1 An illustration of Pappus’s theorem. . . . . . . . . . . . . . . . . . 179
10.2 An illustration of the dual of Pappus’s theorem. . . . . . . . . . . . 179
10.3 Hierarchy of the PObject classes. . . . . . . . . . . . . . . . . . 186
10.4 An illustration of Desargues’ Theorem. . . . . . . . . . . . . . . . 213

14.1 Illustrating a null-terminated character array. . . . . . . . . . . . . 290
14.2 The symbol ⊗ drawn by Program 14.11. . . . . . . . . . . . . . . 322
14.3 Visualizing Pascal’s triangle modulo 2. . . . . . . . . . . . . . . . 324
14.4 An image based on a random process in a triangle. . . . . . . . . . 327
14.5 The Paley graph on 17 vertices. . . . . . . . . . . . . . . . . . . . 330
B.1 Doxygen GUI window. . . . . . . . . . . . . . . . . . . . . . . . . 387
B.2 Doxygen configuration panel. . . . . . . . . . . . . . . . . . . . . 387
xvii

Preface
To my fellow students of mathematics
This book is written for you. This is the book that I wish someone had written for
me. This is a book that introduces the C++ language for people who are interested
in solving mathematical problems.
There is a dizzying selection of books on C++ written for a wide array of au-
diences. Visit your favorite bookseller and you can find C++ books for finance, nu-
merics, computer security, game programming, embedded controllers, graphical user
interfaces, network protocols, data and file structures, engineering, scientific comput-
ing, digital signal processing, simulation, neural nets, artists, virtual machine design,
graphics, computational physics, cellular automata, cryptography, Web agents, busi-
ness, aerospace and flight simulation, music and MIDI instruments, mobile phones,
language translation, computer-aided design, speech recognition, database develop-
ment, computer architecture, photographic imaging, fuzzy logic, hardware control,
rigid body kinematics, real programmers, and—of course—for dummies.
We assume that none of the above applies to you. We approach C++ from the
point of view of solving mathematical problems. We organize our discussion around
the mathematics and bring in the relevant C++ ideas as we need them.
Why C++?
There is a plethora of computer tools available to the mathematical scientist. Many
of these are suited for specific purposes. For example, if you need to perform exten-

sive calculations in support of a number theory problem, the Pari package is perfect.
There is a variety of commercial software packages that are excellent for mathemat-
ical work including Maple, MATLAB, and Mathematica to name but a few.
For many mathematical problems, these systems work perfectly. However, for
problems that require extensive computation the speed of a compiled language such
as C++ cannot be beat. A C++ program can work through billions of examples faster
than most other computing choices.
The length of time it takes to solve a problem on a computer begins not when you
run your program; it begins when you first start to write your program. The object-
oriented nature of C++ enables you to create correct programs quickly. Furthermore,
there is an extensive collection of C++ programs freely available on the Web that can
be customized and used for your purposes (we discuss a few of these in Chapter 13).
In addition, C++ is available for free on most computer systems. See Appendix A
for more information about different versions of C++ for various computing envi-
xix
xx C++ for Mathematicians
ronments (Windows, UNIX, Macintosh).
What can my computer do for me?
Although the utility of computers in the sciences and engineering is unquestion-
able, it is not as clear that a computer is useful for mathematics. However, there are
several arenas in which a computer can be a mathematician’s best friend.
• Symbolic computation. Mathematical work often requires us to fuss with
formidable formulas and solve elaborate equations. Computer algebra systems
such as Maple and Mathematica are extremely useful for such work.
• Visualization. The computer can draw precise pictures and diagrams; often
these images provide key insights to understanding problems.
• Examples and counterexamples. The computer can run through millions of
examples and try myriad possibilities. It is a laboratory in which to perform
experiments to see if ideas work and for discovering patterns.
• Contribution to proof. Sometimes, parts of proofs can be relegated to the

computer for checking. A celebrated example of this is the 1970s-era proof
of the Four Color Theorem by Appel and Haken. More recently, Tom Hales’s
announced proof of the Kepler Conjecture required extensive computation.
I have used the computer in all these ways in my research. Allow me to share an
amusing anecdote in which the computer contributed to the proof of a theorem. I was
working with two colleagues on a problem in discrete mathematics. We knew that we
could complete one portion of the proof if we could find a graph with certain specific
properties. We carefully wrote down these criteria on a blackboard and set out to
find the elusive graph. Although we tried to create the required graph by hand, each
example we tried took us several minutes to check. We realized that the criteria could
be checked mechanically and so we wrote a program to generate graphs at random
until one with the needed properties was found. The first time we ran the program,
it asked us for the number of vertices. Because we were unsuccessful with small
examples, we typed in 50. Nearly instantly we were rewarded when the computer
printed out a few screenfuls of data specifying the graph.
Heartened by this (but not wanting to tangle with such a large graph), we ran the
program again asking for a graph with only 20 vertices. Again, we were greeted
with near instant success. We started to draw the graph on the blackboard, but it was
a nasty random mess (no surprise—our program was designed to examine random
graphs one after another). One more run. Shall we be optimistic? Certain this would
not work, we ran the program again for graphs on 5 vertices. Success! The graph
was actually quite simple and we had a good laugh over how we found our answer.
Preface xxi
Using this book
This book is ideal either for self-study or as a text for a semester-long course
in computer programming (with an emphasis on mathematics). It is important that
undergraduate mathematics majors know how to use the computer effectively. This
is a skill that will serve them well whether for applied scientific/engineering/financial
work, or as a means for forming and testing conjectures in pure research.
We explain how to use C++ from the ground up, however, some experience in

writing programs in any language will help. The reader does not need extensive
experience in programming. Nor is a deep mathematical background needed to
read this book. Our examples are drawn from standard mathematical topics such
as Pythagorean triples [integers (a,b,c) such that a
2
+ b
2
= c
2
] and polynomials.
Whether you are reading this book on your own or in conjunction with a course,
the most effective way to learn is to do. Every chapter ends with a collection
of exercises; do them all. This is important for two reasons. First and foremost,
mastery of any skill requires practice, and learning C++ is no exception. Second,
additional ideas and subtle points are explored in the exercises. To assist you, we
include complete solutions to nearly every exercise in Appendix D.
Organization
We organize our discussion around mathematical themes. For example, Chapters 3
to 5 cover many central ideas in C++ (from how to write procedures to dynamic
allocation of arrays), but a single mathematical problem runs throughout, taking us
from Euclid to Riemann.
The main body of the book is divided into three parts: Procedures, Objects, and
Topics.
Procedures focuses on writing C++ procedures (often called functions by our com-
puter science colleagues, but we have a different meaning for that word). Objects in-
troduces the object-oriented method of programming. If you want to solve a problem
that involves permutations, Chapter 11 shows how C++ can handle these structures
nearly as comfortably as it handles integers. Topics discusses how to use freely
available packages that you can use in your programs, more advanced input/output,
visualization, and selected special features of the C++ language.

Four appendices provide (A) an overview of computing systems (including In-
tegrated Development Environments), (B) the use of the Doxygen documentation
system, (C) a quick reference to the C++ language and supporting libraries, and
(D) answers to nearly every exercise.
No pointers! (almost)
The C++ concepts covered in this book are not exhaustive. There are aspects
of the language that are relevant only to computer scientists and software engi-
neers. For example, C++ provides a number of exotic casting operators (such as
reinterpret_cast
) that are not of interest to mathematicians; we omit those. Nei-
xxii C++ for Mathematicians
ther multiple inheritance nor pure virtual functions are for us. We do not explain how
to make C++ programs work with other languages such as assembly language. We
don’t create our own namespaces. For these and other C++ topics that are useful to
large software engineering projects, please refer to any of several excellent, compre-
hensive C++ reference books.
One topic that we touch only gently is the use of pointers. Mostly, we do not need
them. We successfully avoid this confusing (and errorprone) concept through the
use of call-by-reference and STL container classes. A mathematician shouldn’t be
worrying about exotic data structures such as red–black trees; we just want to insert
and delete elements in a set and not to worry about how the computer manages the
set.
There are, however, a few instances where a rudimentary understanding of pointers
is necessary.
• The name of a C++ array is a pointer to its first element. We need to understand
this when we pass an array to a procedure and when we dynamically allocate
storage for an array.
• Sometimes an object needs to refer to itself (e.g., when a method such as
operator+= returns the value of the object). In these instances, the this
pointer is useful.

We do not provide an extensive discussion of string processing (but do cover the
basics in Chapter 14). As mathematicians, we are interested in getting data into our
programs and results out of them; we are not going to construct word processors.
We omit the exotic and focus on those aspects that make C++ a formidable weapon
in the mathematician’s arsenal. With your brain and this book, your problem doesn’t
stand a chance. Enjoy!
Additional resources
The CD-ROM that comes with this book includes the code for all the numbered
programs (see the List of Programs on page xiii). The programs are free for you to
use under the terms of the GNU General Purpose License. (See the CD-ROM for
details.) The disk also includes solutions to some of the lengthier exercises.
Please visit the Web site for this book www.ams.jhu.edu/˜ers/cpp4m/ where
we maintain a list of errata and submissions for Exercise 1.1.5.
Acknowledgments
Many thanks to my editor Sunil Nair and his helpful staff at Taylor & Francis/CRC
Press. They helped with everything from L
A
T
E
X issues to copy editing to securing
permissions.
Promit Roy is an undergraduate computer science major at Johns Hopkins Univer-
sity. He read through the entire manuscript checking for accuracy and compatibility
Preface xxiii
with Microsoft Visual Studio. In addition, he prepared the MS Visual Studio project
files for the accompanying CD-ROM. Thank you, Promit!
At various points in this book I recommend that the reader consult a “friendly com-
puter science colleague.” I am fortunate to have such a colleague. Thank you, Joanne
Houlahan for your help (and patience) with getting me unstuck from computer woes.
I greatly appreciate my department chair, Daniel Naiman, for his support and en-

couragement for this project.
It gives me great joy to acknowledge the contributions of my father, Ira, and my
son Jonah to the front cover. Grandpa took the cover photo and grandson provided
the design concept.
Most important, thank you to my wife, Amy, and our children, Jonah, Naomi,
Danny, and Rachel, for the world of love and happiness I share with them.
Ed Scheinerman
Baltimore
May 24, 2006

×