| 1 | //#ifndef DEB |
| 2 | #define No_Standard_RangeError |
| 3 | #define No_Standard_OutOfRange |
| 4 | #define No_Standard_DimensionError |
| 5 | //#endif |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | |
| 9 | #include <math_Vector.ixx> |
| 10 | #include <math_Matrix.hxx> |
| 11 | |
| 12 | #include <Standard_DimensionError.hxx> |
| 13 | #include <Standard_DivideByZero.hxx> |
| 14 | #include <Standard_RangeError.hxx> |
| 15 | #include <Standard_NullValue.hxx> |
| 16 | |
| 17 | |
| 18 | math_Vector::math_Vector(const Standard_Integer Lower, |
| 19 | const Standard_Integer Upper): |
| 20 | |
| 21 | LowerIndex(Lower), |
| 22 | UpperIndex(Upper), |
| 23 | Array(Lower,Upper) { |
| 24 | Standard_RangeError_Raise_if(Lower > Upper, ""); |
| 25 | } |
| 26 | |
| 27 | math_Vector::math_Vector(const Standard_Integer Lower, |
| 28 | const Standard_Integer Upper, |
| 29 | const Standard_Real InitialValue): |
| 30 | |
| 31 | LowerIndex(Lower), |
| 32 | UpperIndex(Upper), |
| 33 | Array(Lower,Upper) |
| 34 | { |
| 35 | Standard_RangeError_Raise_if(Lower > Upper, ""); |
| 36 | Array.Init(InitialValue); |
| 37 | } |
| 38 | |
| 39 | math_Vector::math_Vector(const Standard_Address Tab, |
| 40 | const Standard_Integer Lower, |
| 41 | const Standard_Integer Upper) : |
| 42 | |
| 43 | LowerIndex(Lower), |
| 44 | UpperIndex(Upper), |
| 45 | Array(*((const Standard_Real *)Tab), Lower,Upper) |
| 46 | { |
| 47 | Standard_RangeError_Raise_if((Lower > Upper) , ""); |
| 48 | } |
| 49 | |
| 50 | void math_Vector::Init(const Standard_Real InitialValue) { |
| 51 | Array.Init(InitialValue); |
| 52 | } |
| 53 | |
| 54 | math_Vector::math_Vector(const math_Vector& Other): |
| 55 | |
| 56 | LowerIndex(Other.LowerIndex), |
| 57 | UpperIndex(Other.UpperIndex), |
| 58 | Array(Other.Array) {} |
| 59 | |
| 60 | |
| 61 | void math_Vector::SetLower(const Standard_Integer Lower) { |
| 62 | |
| 63 | Array.SetLower(Lower); |
| 64 | UpperIndex = UpperIndex - LowerIndex + Lower; |
| 65 | LowerIndex = Lower; |
| 66 | } |
| 67 | |
| 68 | Standard_Real math_Vector::Norm() const { |
| 69 | |
| 70 | Standard_Real Result = 0; |
| 71 | |
| 72 | for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) { |
| 73 | Result = Result + Array(Index) * Array(Index); |
| 74 | } |
| 75 | return Sqrt(Result); |
| 76 | } |
| 77 | |
| 78 | Standard_Real math_Vector::Norm2() const { |
| 79 | |
| 80 | Standard_Real Result = 0; |
| 81 | |
| 82 | for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) { |
| 83 | Result = Result + Array(Index) * Array(Index); |
| 84 | } |
| 85 | return Result; |
| 86 | } |
| 87 | |
| 88 | Standard_Integer math_Vector::Max() const { |
| 89 | |
| 90 | Standard_Integer I=0; |
| 91 | Standard_Real X = RealFirst(); |
| 92 | |
| 93 | for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) { |
| 94 | if(Array(Index) > X) { |
| 95 | X = Array(Index); |
| 96 | I = Index; |
| 97 | } |
| 98 | } |
| 99 | return I; |
| 100 | } |
| 101 | |
| 102 | Standard_Integer math_Vector::Min() const { |
| 103 | |
| 104 | Standard_Integer I=0; |
| 105 | Standard_Real X = RealLast(); |
| 106 | |
| 107 | for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) { |
| 108 | if(Array(Index) < X) { |
| 109 | X = Array(Index); |
| 110 | I = Index; |
| 111 | } |
| 112 | } |
| 113 | return I; |
| 114 | } |
| 115 | |
| 116 | void math_Vector::Set(const Standard_Integer I1, |
| 117 | const Standard_Integer I2, |
| 118 | const math_Vector &V) { |
| 119 | |
| 120 | Standard_RangeError_Raise_if((I1 < LowerIndex) || |
| 121 | (I2 > UpperIndex) || |
| 122 | (I1 > I2) || |
| 123 | (I2 - I1 + 1 != V.Length()), ""); |
| 124 | |
| 125 | Standard_Integer I = V.Lower(); |
| 126 | for(Standard_Integer Index = I1; Index <= I2; Index++) { |
| 127 | Array(Index) = V.Array(I); |
| 128 | I++; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void math_Vector::Normalize() { |
| 133 | |
| 134 | Standard_Real Result = Norm(); |
| 135 | Standard_NullValue_Raise_if((Result <= RealEpsilon()), ""); |
| 136 | for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) { |
| 137 | Array(Index) = Array(Index) / Result; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | math_Vector math_Vector::Normalized() const { |
| 142 | |
| 143 | math_Vector Result = *this; |
| 144 | |
| 145 | Result.Normalize(); |
| 146 | return Result; |
| 147 | } |
| 148 | |
| 149 | void math_Vector::Invert() { |
| 150 | Standard_Integer J; |
| 151 | Standard_Real Temp; |
| 152 | for(Standard_Integer Index = LowerIndex; |
| 153 | // Index <= LowerIndex + (Length()) >> 1 ; Index++) { |
| 154 | Index <= (LowerIndex + Length()) >> 1 ; Index++) { |
| 155 | J = UpperIndex + LowerIndex - Index; |
| 156 | Temp = Array(Index); |
| 157 | Array(Index) = Array(J); |
| 158 | Array(J) = Temp; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | math_Vector math_Vector::Inverse() const { |
| 163 | math_Vector Result = *this; |
| 164 | Result.Invert(); |
| 165 | return Result; |
| 166 | } |
| 167 | |
| 168 | math_Vector math_Vector::Multiplied(const Standard_Real Right) const{ |
| 169 | |
| 170 | math_Vector Result (LowerIndex, UpperIndex); |
| 171 | |
| 172 | for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) { |
| 173 | Result.Array(Index) = Array(Index) * Right; |
| 174 | } |
| 175 | return Result; |
| 176 | } |
| 177 | |
| 178 | math_Vector math_Vector::TMultiplied(const Standard_Real Right) const{ |
| 179 | |
| 180 | math_Vector Result (LowerIndex, UpperIndex); |
| 181 | |
| 182 | for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) { |
| 183 | Result.Array(Index) = Array(Index) * Right; |
| 184 | } |
| 185 | return Result; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | void math_Vector::Multiply(const Standard_Real Right) { |
| 190 | |
| 191 | for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) { |
| 192 | Array(Index) = Array(Index) * Right; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | |
| 197 | void math_Vector::Divide(const Standard_Real Right) { |
| 198 | |
| 199 | Standard_DivideByZero_Raise_if(Abs(Right) <= RealEpsilon(), ""); |
| 200 | |
| 201 | for(Standard_Integer Index =LowerIndex; Index <=UpperIndex; Index++) { |
| 202 | Array(Index) = Array(Index) / Right; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | |
| 207 | math_Vector math_Vector::Divided (const Standard_Real Right) const { |
| 208 | |
| 209 | Standard_DivideByZero_Raise_if(Abs(Right) <= RealEpsilon(), ""); |
| 210 | math_Vector temp = Multiplied(1./Right); |
| 211 | return temp; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | void math_Vector::Add(const math_Vector& Right) { |
| 216 | |
| 217 | Standard_DimensionError_Raise_if(Length() != Right.Length(), ""); |
| 218 | |
| 219 | Standard_Integer I = Right.LowerIndex; |
| 220 | for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) { |
| 221 | Array(Index) = Array(Index) + Right.Array(I); |
| 222 | I++; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | math_Vector math_Vector::Added(const math_Vector& Right) const{ |
| 227 | |
| 228 | Standard_DimensionError_Raise_if(Length() != Right.Length(), ""); |
| 229 | |
| 230 | math_Vector Result(LowerIndex, UpperIndex); |
| 231 | |
| 232 | Standard_Integer I = Right.LowerIndex; |
| 233 | for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) { |
| 234 | Result.Array(Index) = Array(Index) + Right.Array(I); |
| 235 | I++; |
| 236 | } |
| 237 | return Result; |
| 238 | } |
| 239 | |
| 240 | |
| 241 | |
| 242 | void math_Vector::Subtract(const math_Vector& Right) { |
| 243 | |
| 244 | Standard_DimensionError_Raise_if(Length() != Right.Length(), ""); |
| 245 | |
| 246 | Standard_Integer I = Right.LowerIndex; |
| 247 | for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) { |
| 248 | Array(Index) = Array(Index) - Right.Array(I); |
| 249 | I++; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | |
| 254 | math_Vector math_Vector::Subtracted (const math_Vector& Right) const { |
| 255 | |
| 256 | Standard_DimensionError_Raise_if(Length() != Right.Length(), ""); |
| 257 | |
| 258 | math_Vector Result(LowerIndex, UpperIndex); |
| 259 | |
| 260 | Standard_Integer I = Right.LowerIndex; |
| 261 | for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) { |
| 262 | Result.Array(Index) = Array(Index) - Right.Array(I); |
| 263 | I++; |
| 264 | } |
| 265 | return Result; |
| 266 | } |
| 267 | |
| 268 | |
| 269 | math_Vector math_Vector::Slice(const Standard_Integer I1, |
| 270 | const Standard_Integer I2) const |
| 271 | { |
| 272 | |
| 273 | Standard_RangeError_Raise_if((I1 < LowerIndex) || |
| 274 | (I1 > UpperIndex) || |
| 275 | (I2 < LowerIndex) || |
| 276 | (I2 > UpperIndex) , ""); |
| 277 | |
| 278 | |
| 279 | if(I2 >= I1) { |
| 280 | math_Vector Result(I1, I2); |
| 281 | for(Standard_Integer Index = I1; Index <= I2; Index++) { |
| 282 | Result.Array(Index) = Array(Index); |
| 283 | } |
| 284 | return Result; |
| 285 | } |
| 286 | else { |
| 287 | math_Vector Result(I2, I1); |
| 288 | for(Standard_Integer Index = I1; Index >= I2; Index--) { |
| 289 | Result.Array(Index) = Array(Index); |
| 290 | } |
| 291 | return Result; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | |
| 296 | void math_Vector::Add (const math_Vector& Left, const math_Vector& Right) { |
| 297 | |
| 298 | Standard_DimensionError_Raise_if((Length() != Right.Length()) || |
| 299 | (Right.Length() != Left.Length()), ""); |
| 300 | |
| 301 | |
| 302 | Standard_Integer I = Left.LowerIndex; |
| 303 | Standard_Integer J = Right.LowerIndex; |
| 304 | for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) { |
| 305 | Array(Index) = Left.Array(I) + Right.Array(J); |
| 306 | I++; |
| 307 | J++; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | void math_Vector::Subtract (const math_Vector& Left, |
| 312 | const math_Vector& Right) { |
| 313 | |
| 314 | Standard_DimensionError_Raise_if((Length() != Right.Length()) || |
| 315 | (Right.Length() != Left.Length()), ""); |
| 316 | |
| 317 | Standard_Integer I = Left.LowerIndex; |
| 318 | Standard_Integer J = Right.LowerIndex; |
| 319 | for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) { |
| 320 | Array(Index) = Left.Array(I) - Right.Array(J); |
| 321 | I++; |
| 322 | J++; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | void math_Vector::Multiply(const math_Matrix& Left, |
| 327 | const math_Vector& Right) { |
| 328 | |
| 329 | Standard_DimensionError_Raise_if((Length() != Left.RowNumber()) || |
| 330 | (Left.ColNumber() != Right.Length()), |
| 331 | ""); |
| 332 | |
| 333 | Standard_Integer Index = LowerIndex; |
| 334 | for(Standard_Integer I = Left.LowerRowIndex; I <= Left.UpperRowIndex; I++) { |
| 335 | Array(Index) = 0.0; |
| 336 | Standard_Integer K = Right.LowerIndex; |
| 337 | for(Standard_Integer J = Left.LowerColIndex; J <= Left.UpperColIndex; J++) { |
| 338 | Array(Index) = Array(Index) + Left.Array(I, J) * Right.Array(K); |
| 339 | K++; |
| 340 | } |
| 341 | Index++; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | void math_Vector::Multiply(const math_Vector& Left, |
| 346 | const math_Matrix& Right) { |
| 347 | |
| 348 | Standard_DimensionError_Raise_if((Length() != Right.ColNumber()) || |
| 349 | (Left.Length() != Right.RowNumber()), |
| 350 | ""); |
| 351 | |
| 352 | Standard_Integer Index = LowerIndex; |
| 353 | for(Standard_Integer J = Right.LowerColIndex; J <= Right.UpperColIndex; J++) { |
| 354 | Array(Index) = 0.0; |
| 355 | Standard_Integer K = Left.LowerIndex; |
| 356 | for(Standard_Integer I = Right.LowerRowIndex; I <= Right.UpperRowIndex; I++) { |
| 357 | Array(Index) = Array(Index) + Left.Array(K) * Right.Array(I, J); |
| 358 | K++; |
| 359 | } |
| 360 | Index++; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | void math_Vector::TMultiply(const math_Matrix& TLeft, |
| 365 | const math_Vector& Right) { |
| 366 | |
| 367 | Standard_DimensionError_Raise_if((Length() != TLeft.ColNumber()) || |
| 368 | (TLeft.RowNumber() != Right.Length()), |
| 369 | ""); |
| 370 | |
| 371 | Standard_Integer Index = LowerIndex; |
| 372 | for(Standard_Integer I = TLeft.LowerColIndex; I <= TLeft.UpperColIndex; I++) { |
| 373 | Array(Index) = 0.0; |
| 374 | Standard_Integer K = Right.LowerIndex; |
| 375 | for(Standard_Integer J = TLeft.LowerRowIndex; J <= TLeft.UpperRowIndex; J++) { |
| 376 | Array(Index) = Array(Index) + TLeft.Array(J, I) * Right.Array(K); |
| 377 | K++; |
| 378 | } |
| 379 | Index++; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | void math_Vector::TMultiply(const math_Vector& Left, |
| 384 | const math_Matrix& TRight) { |
| 385 | |
| 386 | Standard_DimensionError_Raise_if((Length() != TRight.RowNumber()) || |
| 387 | (Left.Length() != TRight.ColNumber()), |
| 388 | ""); |
| 389 | |
| 390 | Standard_Integer Index = LowerIndex; |
| 391 | for(Standard_Integer J = TRight.LowerRowIndex; J <= TRight.UpperRowIndex; J++) { |
| 392 | Array(Index) = 0.0; |
| 393 | Standard_Integer K = Left.LowerIndex; |
| 394 | for(Standard_Integer I = TRight.LowerColIndex; |
| 395 | I <= TRight.UpperColIndex; I++) { |
| 396 | Array(Index) = Array(Index) + Left.Array(K) * TRight.Array(J, I); |
| 397 | K++; |
| 398 | } |
| 399 | Index++; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | Standard_Real math_Vector::Multiplied(const math_Vector& Right) const{ |
| 407 | Standard_Real Result = 0; |
| 408 | |
| 409 | Standard_DimensionError_Raise_if(Length() != Right.Length(), ""); |
| 410 | |
| 411 | Standard_Integer I = Right.LowerIndex; |
| 412 | for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) { |
| 413 | Result = Result + Array(Index) * Right.Array(I); |
| 414 | I++; |
| 415 | } |
| 416 | return Result; |
| 417 | } |
| 418 | |
| 419 | math_Vector math_Vector::Opposite() { |
| 420 | math_Vector Result(LowerIndex, UpperIndex); |
| 421 | |
| 422 | for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) { |
| 423 | Result.Array(Index) = - Array(Index); |
| 424 | } |
| 425 | return Result; |
| 426 | } |
| 427 | |
| 428 | math_Vector math_Vector::Multiplied(const math_Matrix& Right)const { |
| 429 | Standard_DimensionError_Raise_if(Length() != Right.RowNumber(), ""); |
| 430 | |
| 431 | math_Vector Result(Right.LowerColIndex, Right.UpperColIndex); |
| 432 | for(Standard_Integer J2 = Right.LowerColIndex; |
| 433 | J2 <= Right.UpperColIndex; J2++) { |
| 434 | Array(J2) = 0.0; |
| 435 | Standard_Integer I2 = Right.LowerRowIndex; |
| 436 | for(Standard_Integer I = LowerIndex; I <= UpperIndex; I++) { |
| 437 | Result.Array(J2) = Result.Array(J2) + Array(I) * |
| 438 | Right.Array(I2, J2); |
| 439 | I2++; |
| 440 | } |
| 441 | } |
| 442 | return Result; |
| 443 | } |
| 444 | |
| 445 | |
| 446 | void math_Vector::Multiply(const Standard_Real Left, |
| 447 | const math_Vector& Right) |
| 448 | { |
| 449 | Standard_DimensionError_Raise_if((Length() != Right.Length()), |
| 450 | ""); |
| 451 | for(Standard_Integer I = LowerIndex; I <= UpperIndex; I++) { |
| 452 | Array(I) = Left * Right.Array(I); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | |
| 457 | math_Vector& math_Vector::Initialized(const math_Vector& Other) { |
| 458 | |
| 459 | Standard_DimensionError_Raise_if(Length() != Other.Length(), ""); |
| 460 | |
| 461 | (Other.Array).Copy(Array); |
| 462 | return *this; |
| 463 | } |
| 464 | |
| 465 | |
| 466 | |
| 467 | void math_Vector::Dump(Standard_OStream& o) const |
| 468 | { |
| 469 | o << "math_Vector of Length = " << Length() << "\n"; |
| 470 | for(Standard_Integer Index = LowerIndex; |
| 471 | Index <= UpperIndex; Index++) { |
| 472 | o << "math_Vector(" << Index << ") = " << Array(Index) << "\n"; |
| 473 | } |
| 474 | } |