13
MATHEMATICAL OBJECTS in Computational Tools in a Unified Object-Oriented Approach Yair Shapira @ CRC Press Taylor & Francis Group Boca Raton London New York CRC Press is an imprint of the Taylor & Francis Croup, an informa business A CHAPMAN St HALL BOOK

MATHEMATICAL OBJECTS in

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: MATHEMATICAL OBJECTS in

MATHEMATICAL OBJECTS in

Computational Tools in a Unified Object-Oriented Approach

Yair Shapira

@ CRC Press Taylor & Francis Group Boca Raton London New York

CRC Press is an imprint of the Taylor & Francis Croup, an informa business

A CHAPMAN St HALL BOOK

Page 2: MATHEMATICAL OBJECTS in

Contents

Part I Numbers* 1

1 Natural Numbers 5 1.1 The Need for Natural Numbers 5 1.2 Mathematical Induction 5 1.3 Unboundedness 6 1.4 Infinity 6 1.5 Adding Natural Numbers 7 1.6 Recursion 8 1.7 The Addition Function 8 1.8 Stack of Calls 9 1.9 Multiplying Natural Numbers 9 1.10 One and Zero 11 1.11 Decimal Representation 12 1.12 Binary Representation 13 1.13 Prime Numbers 14 1.14 Prime Factors 14 1.15 Mathematical Induction in Proofs 15 1.16 The Greatest Common Divisor 16 1.17 Least Common Multiple 17 1.18 The Factorial Function 18 1.19 Exercises 18

2 Integer Numbers 21 2.1 Negative Numbers 21 2.2 The Prisoner Problem 22 2.3 The Integer Numbers 23 2.4 The Number Axis 23 2.5 Angles of Numbers 24 2.6 Exercises 25

3 Rational Numbers 27 3.1 Rational Numbers 27 3.2 The Unique Form 28 3.3 Adding Rational Numbers 28 3.4 Multiplying Rational Numbers 28 3.5 Periodic Decimal Representation 29

Page 3: MATHEMATICAL OBJECTS in

3.6 Diverging Series 31 3.7 The Harmonic Series 32 3.8 Converging Series 32 3.9 Finite Power Series 33 3.10 Infinite Power Series 34 3.11 Periodic Decimal Fractions 35 3.12 Exercises 36

4 Real Numbers 37 4.1 The Square Root of 2 37 4.2 The Least-Upper-Bound Axiom 39 4.3 The Real Numbers 40 4.4 Decimal Representation of v2 41 4.5 Irrational Numbers 42 4.6 Transcendental Numbers 43 4.7 The Natural Exponent 43 4.8 Exercises 45

5 Complex Numbers 47 5.1 The Imaginary Number 48 5.2 The Number Plane 50 5.3 Sine and Cosine 51 5.4 Adding Complex Numbers 51 5.5 Multiplying Complex Numbers 52 5.6 The Sine and Cosine Theorems 53 5.7 Exercises 54

Part II Geometrical Objects* 57

6 Euclidean Geometry 61 6.1 Points and Lines 61 6.2 Rays and Intervals 62 6.3 Comparing Intervals 62 6.4 Ratios between Intervals 63 6.5 Angles 66 6.6 Comparing Angles 66 6.7 Corresponding and Alternate Angles 69 6.8 The Reversed Corresponding-Angle Theorem 70 6.9 Parallel Lines - The Uniqueness Theorem 71 6.10 Triangles 73 6.11 Similar and Identical Triangles 74 6.12 Isosceles Triangles 76 6.13 Pythagoras' Axiom 79 6.14 Sum of Edges 80 6.15 The Longer Edge 81

Page 4: MATHEMATICAL OBJECTS in

6.16 Tales' Theorem 82 6.17 The Reversed Tales' Theorem 84 6.18 Circles 85 6.19 Tangents 89 6.20 Properties of the Tangent 91 6.21 Exercises 93

7 Analytic Geometry 95 7.1 God and the Origin 95 7.2 Numbers and Points 96 7.3 Lines - Sets of Points 97 7.4 Hierarchy of Objects 98 7.5 Half-Planes 98 7.6 Angles 99 7.7 Triangles 100 7.8 Circles 101 7.9 Exercises 101

Part III Composite Mathematical Objects 103

8 Sets 107 8.1 Alice in Wonderland 107 8.2 Sets and Containers 107 8.3 Russell's Paradox 108 8.4 The Empty Set 109 8.5 Sets and Natural Numbers 109 8.6 The Order of the Natural Numbers 109 8.7 Mappings and Cardinality 110 8.8 Ordered Sets and Sequences I l l 8.9 Infinite Sets I l l 8.10 Enumerable Sets 112 8.11 The Set of Integer Numbers 114 8.12 Product of Sets 114 8.13 Equivalence of Sets 115 8.14 The Set of Rational Numbers 117 8.15 Arbitrarily Long Finite Sequences 118 8.16 Function Sets 119 8.17 Cardinality of Function Sets 119 8.18 Nonenumerable Sets 120 8.19 Cardinality of the Real Axis 121 8.20 Cardinality of the Plane 122 8.21 Cardinality of the Multidimensional Space 123 8.22 Larger Cardinalities 124 8.23 Sets of Zero Measure 125 8.24 Cantor's Set 127

Page 5: MATHEMATICAL OBJECTS in

8.25 Exercises 129

9 Vectors and Matrices 131 9.1 Two-Dimensional Vectors 131 9.2 Adding Vectors 132 9.3 Multiplying a Vector by a Scalar 133 9.4 Three-Dimensional Vectors 133 9.5 Multidimensional Vectors 134 9.6 Matrices 135 9.7 Adding Matrices 136 9.8 Multiplying a Matrix by a Scalar 137 9.9 Matrix times Vector 137 9.10 Matrix times Matrix 138 9.11 The Transpose of a Matrix 139 9.12 Symmetric Matrices 140 9.13 Hermitian Matrices 141 9.14 Inner Product 143 9.15 Norms of Vectors 144 9.16 Inner Product and the Hermitian Conjugate 144 9.17 Orthogonal Matrices 145 9.18 Eigenvectors and Eigenvalues 146 9.19 Eigenvalues of a Hermitian Matrix 146 9.20 Eigenvectors of a Hermitian Matrix 147 9.21 The Sine Transform 147 9.22 The Cosine Transform 149 9.23 Determinant of a Square Matrix 149 9.24 Inverse of a Square Matrix 150 9.25 Vector Product 150 9.26 Exercises 151

10 Multilevel Objects 153 10.1 Induction and Deduction 153 10.2 Mathematical Induction 155 10.3 Trees 157 10.4 Binary Trees 158 10.5 Arithmetic Expressions 158 10.6 Boolean Expressions 159 10.7 The Tower Problem 161 10.8 The Tree of the Tower Problem 162 10.9 Pascal's Triangle 163 lO.lOThe Binomial Coefficients 165 lO.HPaths in Pascal's Triangle 166 10.12Paths and the Binomial Coefficients 168 10.13Newton's Binomial 169 10.14Brownian Motion 170

Page 6: MATHEMATICAL OBJECTS in

10.15Counting Integer Vectors 171 lO.lGMathematical Induction in Newton's Binomial 177 10.17Factorial of a Sum 178 10.18The Trinomial Formula 180 10.19Multiscale 181 10.20The Decimal Representation 182 10.21The Binary Representation 182 10.22The Sine Transform 182 10.23Exercises 184

11 G raphs 187 11.1 Oriented Graphs 187 11.2 Nonoriented Graphs 188 11.3 The Node-Coloring Problem 189 11.4 The Node-Coloring Algorithm 190 11.5 The Edge-Coloring Problem 191 11.6 The Edge-Coloring Algorithm 192 11.7 Graph of Edges 193 11.8 Triangulation 194 11.9 The Triangle-Coloring Problem 195 ll.lOWeighted Graphs 196 11.11 Algebraic Formulation 198 11.12The Steady State 199 11.13Exercises 200

12 Polynomials 203 12.1 Adding Polynomials 203 12.2 Multiplying a Polynomial by a Scalar 204 12.3 Multiplying Polynomials 204 12.4 Computing a Polynomial 206 12.5 Composition of Polynomials 207 12.6 Natural Numbers as Polynomials 208 12.7 Computing a Monomial 209 12.8 Derivative 210 12.9 Indefinite Integral 210 12.10Integral over an Interval 210 12.11Sparse Polynomials 211 12.12Composition of Sparse Polynomials 212 12.13Polynomials of Two Variables 213 12.14Partial Derivatives 214 12.15The Gradient 215 12.16Integral over the Unit Triangle 215 12.17Second Partial Derivatives 217 12.18Degree 218 12.19Polynomials of Three Variables 218

Page 7: MATHEMATICAL OBJECTS in

12.20Partial Derivatives 218 12.21The Gradient 219 12.22Integral over the Unit Tetrahedron 220 12.23Directional Derivatives 220 12.24Normal Derivatives 221 12.25Tangential Derivatives 223 12.26High-Order Partial Derivatives 223 12.27The Hessian 224 12.28Degree 225 12.29Degrees of Freedom 225 12.30Basis Functions in the Unit Tetrahedron 226 12.31Computing the Basis Functions 227 12.32Composite Functions in a General Tetrahedron 230 12.33The Chain Rule 232 12.34Directional Derivative of a Composite Function 232 12.35The Hessian of a Composite Function 233 12.36Basis Functions in a General Tetrahedron 233 12.37Tensors 234 12.38Hessian-Related Basis Functions 236 12.39Basis Functions at Edge Midpoints 237 12.40Basis Functions at Side Midpoints 240 12.41 Continuity 240 12.42Continuity of Gradient 241 12.43Integral over a General Tetrahedron 242 12.44Exercises 243

Part IV Introduction to С 247

13 Basics of Programming 251 13.1 The Computer and its Memory 251 13.2 The Program or Code 252 13.3 The Code Segments in this Book 253 13.4 Variables and Types 254 13.5 Defining Variables 256 13.6 Assignment 256 13.7 Initialization 257 13.8 Explicit Conversion 258 13.9 Implicit Conversion 259 13.10Arithmetic Operations 259 13.11Functions 261 13.12The "Main" Function 264 13.13Printing Output 264 13.14Comparison Operators 265 13.15Boolean Operators 266 13.16The "?:" Operator 266

Page 8: MATHEMATICAL OBJECTS in

13.17Conditional Instructions 267 13.18Scope of Variables 268 13.19Loops 270 13.20The Power Function 273 13.21Integer Logarithm 273 13.22The Factorial Function 274 13.23Nested Loops 274 13.24Reversed Number 275 13.25Binary Number 276 13.26Pointers 277 13.27Pointer to a Constant Variable 278 13.28The Referencing Operator 278 13.29Arrays 279 13.30Two-Dimensional Arrays 279 13.31 Passing Arguments to Functions 280 13.32Input/Output (I/O) 281 13.33Input/Out with Files 282 13.34Exercises 283

14 Recursion 285 14.1 Recursive Functions 285 14.2 The Power Function 286 14.3 Integer Logarithm 287 14.4 The Factorial Function 287 14.5 Ordered Arrays 287 14.6 Binary Representation 290 14.7 Pascal's Triangle 291 14.8 Arithmetic Expression 292 14.9 Static Variables 297 14.10The Exponent Function 298 14.11 Exercises 300

Part V Introduction to C + + 303

15 Objects 307 15.1 Classes 307 15.2 Private and Public Members 308 15.3 Interface Functions 310 15.4 Information and Memory 311 15.5 Constructors 312 15.6 Initialization List 313 15.7 Default Arguments 314 15.8 Explicit Conversion 314 15.9 Implicit Conversion 315 15.10The Default Copy Constructor 315

Page 9: MATHEMATICAL OBJECTS in

15.11 Destructor 317 15.12Member and Friend Functions 318 15.13The Current Object and its Address 319 15.14Returned Pointer 320 15.15Pointer to a Constant Object 320 15.16References 321 15.17Passing Arguments by Reference 322 15.18Returning by Reference 322 15.19Efficiency in Passing by Reference 323 15.20Copy Constructor 324 15.21 Assignment Operators 325 15.220perators 326 15.23Inverse Conversion 327 15.24Unary Operators 328 15.25Update Operators 328 15.26Friend Update Operators 329 15.27Binary Operators 330 15.28Friend Binary Operators 331 15.29Member Binary Operators 331 15.30Ordinary Binary Operators 332 15.31Complex Numbers 332 15.32Member Operators with Complex Numbers 334 15.330rdinary Operators with Complex Numbers 336 15.34Exercises 338

16 Vectors and Matr ices 339 16.1 Induction and Deduction in Object-Oriented Programming . . 339 16.2 Templates 340 16.3 The Vector Object 341 16.4 Constructors 343 16.5 Assignment Operators 344 16.6 Arithmetic Operators 345 16.7 Points in the Cartesian Plane and Space 347 16.8 Inheritance 347 16.9 Public Derivation 348 16.10Protected Members of the Base Class 349 16.11 Constructing a Derived Object 350 16.12Functions of Derived Objects 350 16.13Destroying a Derived Object 351 16.14Inherited Member Functions 351 16.150verridden Member Functions 351 16.16The Matrix Object 351 16.17Power of a Square Matrix 354 16.18Exponent of a Square Matrix 355 16.19Exercises 356

Page 10: MATHEMATICAL OBJECTS in

17 Dynamic Vectors and Lists 359 17.1 Dynamic Vectors 359 17.2 Ordinary Lists 365 17.3 Linked Lists 369 17.4 The Copy Constructor 371 17.5 The Destructor 372 17.6 Recursive Member Functions 372 17.7 Inserting New Items 373 17.8 The Assignment Operator 374 17.9 Dropping Items 375 17.10The Merging Problem 377 17.11 The Ordering Problem 380 17.12Stacks 382 17.13Exercises 384

Part VI Implementation of Computational Objects 387

18 Trees 391 18.1 Binary Trees 391 18.2 Recursive Definition 392 18.3 Implementation of Binary Tree 392 18.4 The Destructor 395 18.5 The Tower Constructor 398 18.6 Solving the Tower Problem 400 18.7 General Trees 403 18.8 Exercises 404

19 Graphs 405 19.1 The Matrix Formulation 405 19.2 The Node-Coloring Algorithm 406 19.3 The Edge-Coloring Algorithm 406 19.4 Sparse Matrix 408 19.5 Data Access 409 19.6 Virtual Addresses 410 19.7 Advantages and Disadvantages 413 19.8 Nonoriented Graphs 414 19.9 Exercises 416

20 Sparse Matrices 419 20.1 The Matrix-Element Object 419 20.2 Member Arithmetic Operators 421 20.3 Comparison in Terms of Column Index 423 20.4 Ordinary Arithmetic Operators 423 20.5 The Row Object 425 20.6 Reading the First Element 426

Page 11: MATHEMATICAL OBJECTS in

20.7 Inserting a New Element 427 20.8 Recursive Functions 428 20.9 Update Operators 429 20.10Member Binary Operators 430 20.11Ordinary Binary Operators 431 20.12The Sparse-Matrix Object 432 20.13Reading a Matrix Element 434 20.14Some More Member Functions 435 20.15The Node-Coloring Code 436 20.16Edge Coloring in a Nonoriented Graph 438 20.17Edge Coloring in an Oriented Graph 440 20.18Exercises 444

21 Meshes 445 21.1 The Node Object 446 21.2 Reading and Accessing Data Fields 447 21.3 The Cell - a Highly Abstract Object 449 21.4 The Cell Object 451 21.5 Reading and Accessing Vertices 452 21.6 Constructors 453 21.7 The Assignment Operator 455 21.8 Nodes in a Cell 456 21.9 Edge-Sharing Cells 457 21.10The Mesh Object 458 21.11Indexing the Nodes 460 21.12An Example 461 21.13Indexing the Cells 463 21.14Exercises 464

22 Triangulation 465 22.1 Triangulation of a Domain 466 22.2 Multilevel Iterative Mesh Refinement 466 22.3 Dividing a Triangle and its Neighbor 467 22.4 Refining the Mesh 470 22.5 Approximating a Circle 472 22.6 The Cell-Coloring Code 476 22.7 The Matrix Formulation 477 22.8 The Code-Size Rule 479 22.9 Exercises 480

Par t VII Three-Dimensional Applications 483

Page 12: MATHEMATICAL OBJECTS in

23 Mesh of Tetrahedra 487 23.1 The Mesh Refinement 487 23.2 Refining the Neighbor Tetrahedra 488 23.3 The Refinement Step 489 23.4 Exercises 491

24 Polynomials 493 24.1 The Polynomial Object 493 24.2 Adding Polynomials 495 24.3 Multiplication by a Scalar 496 24.4 Multiplying Polynomials 497 24.5 Calculating a Polynomial 498 24.6 Composition of Polynomials 499 24.7 Recursive Horner Code 500 24.8 Polynomials of Two Variables 501 24.9 Polynomials of Three Variables 501 24.10Indefinite Integral 502 24.11Integral on the Unit Interval 502 24.12Integral on the Unit Triangle 503 24.13Integral on the Unit Tetrahedron 503 24.14Exercises 504

25 Sparse Polynomials 509 25.1 The Monomial Object 509 25.2 Multiplying Monomials 511 25.3 The Sparse-Polynomial Object 511 25.4 Multiplying a Sparse Polynomial by a Scalar 513 25.5 Multiplying Sparse Polynomials 514 25.6 Adding Sparse Polynomials 515 25.7 The Modified Horner Code 516 25.8 Polynomials of Two Variables 518 25.9 Polynomials of Three Variables 519 25.10Exercises 519

26 Stiffness and Mass Matrices 521 26.1 The Neumann Matrix 521 26.2 The Boundary Matrix 522 26.3 The Stiffness Matrix 523 26.4 The Mass Matrix 524 26.5 Newton's Mass Matrix 524 26.6 Helmholtz Mass Matrix 525 26.7 Helmholtz Matrix 528 26.8 Newton's Iteration 528 26.9 Dirichlet Boundary Conditions 529 26.10Exercises 531

Page 13: MATHEMATICAL OBJECTS in

27 Splines 535 27.1 The Indexing Scheme 535 27.2 Basis Functions in the Mesh 536 27.3 The Neumann Matrix 539 27.4 The Spline Problem 540 27.5 The Dirichlet Matrix 541 27.6 Exercises 541

28 Appendix: Solutions of Exercises 543 28.1 Representation of Integers in any Base 543 28.2 Prime Factors 543 28.3 Greatest Common Divisor 544 28.4 Recursive Implementation of Ca,n 545 28.5 Square Root of a Complex Number 545 28.6 Operations with Vectors 547 28.7 Operations with Matrices 549 28.8 Determinant, Inverse, and Transpose of 2 x 2 Matrix 551 28.9 Determinant, Inverse, and Transpose of 3 x 3 Matrix 552 28.10Vector Product 553 28.11The Matrix Exponent Function 553 28.120perations with Dynamic Vectors 555 28.13Using the Stack Object 557 28.140perations with Sparse Matrices 557 28.15Three-Dimensional Mesh 560 28.161ntegrals over the Tetrahedron 562 28.17Computing Partial Derivatives 563 28.18Composing Sparse Polynomials 565 28.19Calculations with Sparse Polynomials 566 28.20The Stiffness Matrix 568 28.21Newton's Mass Matrix 571 28.22Helmholtz Mass Matrix 572 28.23Indexing the Edges in the Mesh 575 28.24Indexing the Sides in the Mesh 578 28.25Computing Basis Functions 580 28.26Setting Dirichlet Conditions 588

References 593

Index 595