Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

C++ For Mathematicians (2006) [eng]

.pdf
Скачиваний:
211
Добавлен:
16.08.2013
Размер:
31.64 Mб
Скачать

486

C++ for Mathematicians

 

 

PHI = 1.61803

 

 

 

 

TeX form: \frac{\left(1 + \sqrt{5}\right)}{2}

 

 

(1+

 

 

 

 

 

) .

The T X code produces this:

5

E

2

 

 

 

Showing that two Constructible objects represent the same complex number is tricky so we did not define an == operator. See E.R. Scheinerman, “When close enough is close enough,” American Mathematical Monthly 107 no. 6 (June–July 2000) 489–499 for one approach.

15.6A full solution is available on the accompanying CD-ROM. In this solution we use several classes.

Token: Objects of this type are “atoms” of a Boolean expression. This may be a variable, a constant, a unary operation, or a binary operation.

Expression: Objects of this type represent Boolean expressions.

LookupTable: These are devices that associate Boolean values with variables.

ExpressionLoader: This is a device to read input streams and produce

Expression objects.

TautologyChecker: This is a device that runs through all possible substitutions for the variables in an Expression to see if any yield FALSE. If not, then the expression is a tautology.

Index

!, 22, 394 !=, 22, 104, 394 ϕ, 67

*, 13, 18, 71, 130, 351–353, 394, 395

*/, 7

*=, 20 +, 18, 292, 394

++, 20, 131, 394, 404 +=, 20, 394 ,, 395 -, 18, 394

--, 20, 131, 394, 404 -=, 20 ->, 142, 356, 395

., 395

..., 342 /, 18, 394

/*, 7

/**, 382

//, 7

///, 382 /=, 20 :, 182, 337

::, 7, 99, 101 ;, 5, 95, 396

<, 22, 236, 238, 394 <<, 5, 104, 107, 259, 303, 395 <=, 22, 394 =, 12, 394, 404

==, 14, 22, 104, 394 >, 22, 236, 238, 394 >=, 22, 394 >>, 260, 303, 395

?:, 224, 395, 396 $, 366 %, 19, 394

%=, 20 &, 47, 97, 105, 259, 350, 352, 395

&&, 22, 394 ˆ, 22, 259, 395 {, 396 }, 396

˜, 220, 395, 402 |, 259, 395 ||, 22, 394

a.exe, 366, 367 a.out, 4, 366, 367 abort, 342, 413 abs, 409

acos, 409 adjacent, 326

advancing statement, 36 algorithm, 124

-ansi, 368, 371 any, 262

arbitrary precision arithmetic, 269 argc, 298

argv, 298

arithmetic operator, 18–20 array, 69–71, 393

character, 5, 289–291, 306, 471 smart, 233, 264 two-dimensional, 273–274

ASCII, 289 asin, 409

assignment, 12, 100, 394 operator, 131, 221–223, 404

with arithmetic operation, 20, 394 at, 135, 294

atan, 409 atan2, 102, 409 atod, 299

487

488

C++ for Mathematicians

atof, 299, 413 atoi, 299, 413, 471 atol, 299, 413

@author, 390

back, 149

bad alloc, 359, 480 base name, 5

bash, 364–366

begin, 130, 131, 141, 144 bell curve, 61

Bessel functions, 409

binomial coefficient, 153, 287, 322 bitset, 260, 261

bool, 14, 391 boolalpha, 311 Boolean expression, 360 Boost, 286, 451 Box–Muller method, 62 break, 42, 334, 337 break point, 375, 377 @brief, 25, 382, 389 Buffon’s needle, 64

@bug, 390

bus error, 353

.C, 5

C++ Java Matrix Library, 274 C++, as an extension of C, 21 c str, 297, 471

call by reference, 47, 97, 104, 105, 108, 132, 139, 169, 221, 276, 346, 356, 395, 399, 419

call by value, 35, 46, 72, 104, 276, 346, 399

capacity, 136 case, 334, 397 cast, 19, 44, 100

catch, 338, 339, 398 catch all, 342 Cauchy sequence, 98 cbrt, 409

.cc, 5, 399 cctype, 331 cd, 366

ceil, 409

cerr, 34, 108, 300 cfloat, 16 CGAL, 286

char, 14, 289, 391 character

array, 5, 289–291, 306, 471 control, 412

string, 289

Chinese Remainder Theorem, 68, 88, 174

Cholesky factorization, 278–280, 284 CHomP, 287

cin, 20, 300 circle, 320 class, 93, 401–408

container, 127 template, 238–242, 407

class, 96

clear, 128, 136, 140, 144, 303 climits, 16

clique, 330 close, 302 closepl, 320

cmath, 21, 64, 94, 97, 102, 112, 123, 408

@code, 389 code file, 94, 399 colon, 182, 337 comma, 395

command line argument, 297–300 comment, 7

comparison operator, 22 compiled language, 4 compiler, 4

complex, 23, 238 complex number, 23–27 complexx, 24

Computational Geometry Algorithms Library, 286

Computational Homology Program, 287 concatenation, 292

condition number, 280

const, 25, 58, 82, 83, 102, 105, 108, 132, 147, 167, 276, 297,

Index

393, 400

const iterator, 132, 145, 147 constant, global, 25, 165 constructible number, 359 constructor, 96, 99, 116, 402

as type converter, 100 copy, 219, 221, 261, 405

container class, 127 continue, 42 control character, 412 copy, 275

copy constructor, 219, 221, 261, 405 cos, 64, 410

cosh, 410

count, 128, 140, 262 cout, 5, 34, 107, 108, 300 csh, 364

cstdlib, 54, 56, 413 cstring, 291 ctime, 56, 414 ctype, 411, 412 cube root, 409

.cxx, 5 CXXFLAGS, 371 Cygwin, 364

data hiding, 96, 97

@date, 390 dec, 311 declaration, 12

decreasing subsequence, 215 decrement operator, 20, 404 Dedekind cut, 98 default, 334, 397

default parameters, 195, 400

#define, 26 delete, 355–356

delete[], 81, 124, 127, 134, 135, 219, 223, 273, 353, 355, 393

deprecated, 274 deque, 149

dereference, 107, 130, 351, 352 derivative, 265

Desargues’ Theorem, 212 destructor, 220, 402

489

determinant, 279, 284

disjoint cycle notation, 215, 225 do, 41, 42, 397

double, 14, 391

double inclusion, 26, 33, 96

Doxyfile, 388

Doxygen, xxi, 25, 33, 364, 381–390 duality, in RP2, 178

edge, 326

eigenvalue, 278, 283, 284 else, 76, 396

emacs, 363–365 c++-mode, 365

empty, 128, 136, 140, 144, 148, 294 end, 131, 133, 141, 144

end of file, 304

@endcode, 389 #endif, 26 endl, 6

enum, 348, 481 enumeration, 348 epsilon, 16

equality operator, 14 erase, 128, 140, 145, 293

Erdos˝–Szekeres Theorem, 215, 229 erf, 410

Euclid’s method, 37, 247 Euler

rhymes with, 9 totient, see totient

exception, 135, 338, 398 executable file, 4 exit, 33, 341, 413 exp, 21, 394, 410 exponentiation, 21 extension, 5

@f[, 390 @f$, 390 @f], 390 fabs, 112 factorial, 39 factoring, 71

fail, 301–304

490

falling through cases, 334 false, 261

fast Fourier transform, 284 fcircle, 320 Fibonacci, 49

file

ASCII, 301 binary, 301 code, 94, 399 executable, 4 extension, 5

header, 6, 24, 32, 94, 399 object, 4

plain text, 301 source, 4 stream, 301 tar, 316

@file, 25, 382, 389 fillcolorname, 320, 328 filltype, 320, 328 find, 133, 295

find first of, 296 find last of, 296 first, 139, 142 fixed, 311

fline, 320 flinewidth, 319 flip, 262 float, 14, 391 floor, 64, 410 flowchart, 73 fmod, 410

for, 36, 42, 396 for each, 153 free group, 331

Free Software Foundation, 363 friend, 344–347, 406 front, 149

fspace, 319 fstream, 301 function, 31

identity, 480 member, 94 rational, 265 signum, 49

C++ for Mathematicians

-g, 369

g++, 4, 363, 364 gamma function, 410 Gaussian

integer, 23, 115, 238, 391 poetry, 6

random variable, 61 gcd, 31

extended, 45–49 polynomial, 247

get, 305

get method, 98 getline, 306 GIFPlotter, 322, 325 global

constant, 25, 165 variable, 25, 164, 165

GMP, 269, 286

gmp randclass, 270

GNU Multiple Precision Library, 269, 286

Gnu software, 363 good, 301–304 goto, 336–337, 398 gprof, 369

graph, 326 simple, 327

graphics, 315

greatest common divisor, see gcd group, free, 331

H, 176

.h, 399

header file, 6, 24, 32, 94, 399 heap, 356

Hello world, 4 hex, 311

Hilbert matrix, 280, 284 hypot, 410

-I, 272, 282, 369 IDE, 4, 363, 372 identity function, 480 if, 34, 76, 396

#ifndef, 26

Index

ifstream, 301 imaginary part, 23 #include, 6, 99, 369 increasing subsequence, 215 increment operator, 20, 404 independent set, 330

inf, 338, 416

infinity, 212, 338, 348, 416 line at, 177, 178, 188 point at, 177

inheritance, 177, 181

inline, 119, 165, 168, 196, 236, 241, 401, 435

inline procedure, 119 insert, 128, 140, 145, 293 int, 11, 391

int64, 12, 391

INT MAX, 16

INT MIN, 16 integer

factoring, 71

Gaussian, 23, 115, 238, 391 integrated development environment,

4, 363, 372 interval, 125

random, 126 iomanip, 86, 309 ios::app, 302 ios::floatfield, 311 iostream, 6, 7, 34, 107 isalnum, 412 isalpha, 412 iscntrl, 412 isdigit, 412 isgraph, 412

isinf, 338 islower, 412 isnan, 338 isprint, 412 ispunct, 412 isspace, 412

istringstream, 307, 471 isupper, 331, 412 isxdigit, 412

iterator, 129

491

deque, 149 list, 144 map, 141 read only, 147

set, 130–133, 153, 447 vector, 446

JAMA, 274 join, 326

key/value pair, 139 Koch curve, 332

-L, 272, 282, 369 -l, 272, 282, 369 label, 337

LCG, 53, 57–59 least squares, 279 LEDA, 287 left, 310

left shift operator, 5 length, 294

lexicographic ordering, 117, 126, 139, 294

library, 272, 282, 320, 369

Library of Efficient Data Structures and

Algorithms, 287 LiDIA, 286

line, 320

linear congruential generator, 53, 57– 59

linear fractional transformation, 234 linear system of equations, 279

LineParser, 312 linker, 4

linking, 368 list, 144–148 loader, 4

log, 410 log10, 410

logical operator, 22, 394 long, 12, 391

long double, 14, 391 long long, 12, 391 look-up table, 142, 445

492

C++ for Mathematicians

loop statements, 41 ls, 366

LU-factorization, 278–280

Mconstants, 411

MPI, 21, 97 main, 8, 32

make, 317, 364, 368, 370–372 make pair, 139, 140 Makefile, 370

Mandelbrot set, 332 manipulator, 309 map, 139–144 matrix inverse, 279

matrix multiplication, 277 Matrix Template Library, 286

max size, 136 median, 126, 264 member functions, 94

memory leak, 81, 90, 135, 220, 358, 402, 427

method, 32, 93, 94 get, 98

private, 117, 167 set, 98

static, 157, 163, 166, 232 million/billion/trillion rule of thumb,

61

mod operator, 19–20 monic, 247

monotone subsequence, 215 Monte Carlo, 60

mpf class, 270 mpq class, 270 mpz class, 270 multiline comment, 7 multimap, 144 multiple inclusion, 187 multiset, 133–134 mycomplex, 239

N(0,1), 62 namespace, 274

standard, 7 nan, 338, 416

National Institute of Standards and Technology, 274

new, 80, 81, 124, 273, 353, 355–356, 393

newmat, 282–286 Newton’s method, 265 noboolalpha, 311 none, 262

normal random variable, 61, 62 noshowpoint, 309

not a number, 338, 416 nouppercase, 311 npos, 295

null terminated, 289–290 number

complex, 23–27 constructible, 359 random, 53

uniform, 54

-O, 369 object, 93 object file, 4 oct, 311

ofstream, 301

On-Line Encyclopedia of Integer Sequences, 87, 442

open, 302 openpl, 319 operator, 97

address-of, 350 arithmetic, 18–20

assignment, 131, 221–223, 404 bitwise, 259–260

comparison, 22 decrement, 20, 404 equality, 14 increment, 20, 404 left shift, 5 logical, 22, 394 mod, 19–20 overloading, 104 relational, 167 right shift, 20

operator, 105, 224, 403–404

order, of a permutation, 232 ordered pair, 138–139, 441 ostream, 107, 108 ostringstream, 307 overflow, 13, 50, 171, 417 overloading

operator, 104 procedure, 46, 54

override, 183

-p, 369

pair, 138, 230, 239, 441 Paley graph, 326–330 Pappus’s Theorem, 178, 207 @param, 33, 383, 389 partition, 152

perfect, 442

Pascal’s triangle, 287, 322, 445 identity, 153

PDP-8, 3

-pedantic, 368, 371 pencolor, 319 pencolorname, 319 permutation, 215

order, 232 -pg, 369

pigeonhole principle, 216

Plotter, 317 plotutils, 316–330 poetry, bad, 4

pointer, xxii, 70, 124, 350–358 dereferencing, 107, 130

polar method, 62 polygram, 331 polynomial, 235

derivative, 265 monic, 247

pop, 148–150

pop back, 145, 149 pop front, 145, 149 positive definite, 279 postdecrement, 21 postincrement, 21 pow, 21, 394, 410 predecrement, 21

Index

493

preincrement, 21 preprocessor, 7

prime number theorem, 80 primitive Pythagorean triple, 115 priority queue, 150 private, 402

private, 97, 104, 182 procedure, 8, 32

friend, 344–347 inline, 119 overloading, 46 schema, 237

template, 235–238, 277, 400 prof, 369

profiling tool, 369 project, 372

projective plane, 177–178 protected, 184, 408

pseudo random number generator, 53, 271

PSPlotter, 318 public, 402 public, 97, 98, 182 push, 148, 150

push back, 137, 145, 149 push front, 144, 149 put, 306

pwd, 366

Pythagorean triple, 115 primitive, 115

QR-factorization, 278, 279, 284 quadratic residue, 327 quaternion, 176

queue, 148 quotation mark, 6

rand, 54, 55, 414 RAND MAX, 54, 55, 414 random

interval, 126 variable

Gaussian, 61 normal, 61, 62

walk, 65

494

C++ for Mathematicians

randomized algorithm, 45 rank, 279

rational function, 265 rbegin, 131

real part, 23 recursion, 39

infinite, 39 reference, 47, 107, 147 remove, 145 remove if, 145, 147 rend, 131 replace, 293 reserve, 136, 245 reset, 262 resize, 135 rethrow, 343

return

by reference, 464 by value, 464 type, 33

@return, 33, 383, 389 return, 33, 34, 47, 398 reverse Polish notation, 360 rfind, 296

right, 310

right shift operator, 20 roundoff, 11, 14, 201, 419 RP2, 177–178

RPN, 360

schema, 237 scientific, 311 scientific notation, 14 second, 139, 142

@see, 389

seed, 53, 55, 56, 271 segmentation fault, 353 Seldon, 286

semicolon, 5, 95, 396 set

independent, 330 Mandelbrot, 332

set, 127–133, 262 set method, 98 setfill, 310

setiosflags, 311 setprecision, 86, 309 setw, 310

sh, 364 short, 12, 391

showpoint, 309 showpos, 311

Sierpinski’s triangle, 324, 326 Sieve of Eratosthenes, 78, 79, 137 signum, 49

simple graph, 327 simplicial complex, 154 sin, 410

single-line comment, 7

singular value decomposition, 278, 284 sinh, 411

size, 128, 135, 140, 144, 149, 262, 294

size t, 128

sizeof, 12, 15, 355, 395 Sloane, Neil, 87

sneaky trick, 22, 218 sort, 123, 126, 145, 153

sort requires algorithm, 124 source file, 4

space, 319

sqrt, 94, 112, 123, 411 srand, 56, 414 sstream, 307

stack, 355, 447 stack, 148

standard namespace, 7 starting statement, 36 statement

advancing, 36 catch, 339 declaration, 12 label, 337 starting, 36 terminator, 5 throw, 339

static, 58, 63, 143, 164, 166, 392, 405, 424, 428

three usages, 166 std::cout, 7

Index

str, 307 strcat, 291 strcmp, 291 strcpy, 291

string, 15, 261, 289, 291–297, 471 string stream, 307

strlen, 290 strncat, 291 strncmp, 291 strncpy, 291 struct, 347 structure, 347 subclass

private, 182 public, 182

subscript, 70 subsequence, 215 substr, 294 SVD, 278, 284

switch, 333–336, 397 Sylvester’s four-point problem, 64 symmetric matrix, 279, 283 system header file, 6

tan, 411 tanh, 411 target, 371 tautology, 360 template, 133

class, 238–242, 407 procedure, 235–238, 277, 400

Template Numerical Toolkit, 274 test, 262

do not name your program, 380 test condition, 36

text editor, 4

this, xxii, 106, 107, 168, 170, 222, 223, 350, 358, 406, 434, 435

throw, 338, 339, 344, 398 tilde, 220

time, 56, 271, 414 time/memory tradeoff, 143 time t, 56

TNT, 274 tolower, 413

495

top, 148, 150

totient, 31, 45, 67–69, 76, 83, 85 toupper, 331, 413

trace, 277, 284 trigraph, 224, 395 try, 338, 339, 398 twos complement, 258 type, 11

return, 33 typedef, 23, 349, 458

Ulam’s problem, 215, 229, 231 unary minus, 170

unicode, 289 unif, 54, 55

uniform random number, 54 union, 133, 348 unique, 145

unsigned, 13, 391 uppercase, 311 user header file, 6

using namespace std, 7, 380 utility, 138, 230

variable assignment, 12 class

static, 166 declaration, 12 global, 25, 164, 165 reference, 47, 147 static, 143, 166

class, 157, 163, 164

vector, 134–138, 218, 239, 243, 245, 273

vector<bool>, 136 @version, 390 vertex, 326

view, 275

Visual Studio, 372–376 visualization, 315

void, 46, 99, 102, 103, 132, 398 volatile, 27

-Wall, 368, 371