comparison src/DLD-FUNCTIONS/bsxfun.cc @ 10154:40dfc0c99116

DLD-FUNCTIONS/*.cc: untabify
author John W. Eaton <jwe@octave.org>
date Wed, 20 Jan 2010 17:33:41 -0500
parents e409546ac0a8
children 816b91eebe79
comparison
equal deleted inserted replaced
10153:2c28f9d0360f 10154:40dfc0c99116
220 return retval; 220 return retval;
221 } 221 }
222 222
223 static bool 223 static bool
224 maybe_update_column (octave_value& Ac, const octave_value& A, 224 maybe_update_column (octave_value& Ac, const octave_value& A,
225 const dim_vector& dva, const dim_vector& dvc, 225 const dim_vector& dva, const dim_vector& dvc,
226 octave_idx_type i, octave_value_list &idx) 226 octave_idx_type i, octave_value_list &idx)
227 { 227 {
228 octave_idx_type nd = dva.length (); 228 octave_idx_type nd = dva.length ();
229 229
230 if (i == 0) 230 if (i == 0)
231 { 231 {
232 idx(0) = octave_value (':'); 232 idx(0) = octave_value (':');
233 for (octave_idx_type j = 1; j < nd; j++) 233 for (octave_idx_type j = 1; j < nd; j++)
234 { 234 {
235 if (dva (j) == 1) 235 if (dva (j) == 1)
236 idx (j) = octave_value (1); 236 idx (j) = octave_value (1);
237 else 237 else
238 idx (j) = octave_value ((i % dvc(j)) + 1); 238 idx (j) = octave_value ((i % dvc(j)) + 1);
239 239
240 i = i / dvc (j); 240 i = i / dvc (j);
241 } 241 }
242 242
243 Ac = A; 243 Ac = A;
244 Ac = Ac.single_subsref ("(", idx); 244 Ac = Ac.single_subsref ("(", idx);
245 return true; 245 return true;
246 } 246 }
248 { 248 {
249 bool is_changed = false; 249 bool is_changed = false;
250 octave_idx_type k = i; 250 octave_idx_type k = i;
251 octave_idx_type k1 = i - 1; 251 octave_idx_type k1 = i - 1;
252 for (octave_idx_type j = 1; j < nd; j++) 252 for (octave_idx_type j = 1; j < nd; j++)
253 { 253 {
254 if (dva(j) != 1 && k % dvc (j) != k1 % dvc (j)) 254 if (dva(j) != 1 && k % dvc (j) != k1 % dvc (j))
255 { 255 {
256 idx (j) = octave_value ((k % dvc(j)) + 1); 256 idx (j) = octave_value ((k % dvc(j)) + 1);
257 is_changed = true; 257 is_changed = true;
258 } 258 }
259 259
260 k = k / dvc (j); 260 k = k / dvc (j);
261 k1 = k1 / dvc (j); 261 k1 = k1 / dvc (j);
262 } 262 }
263 263
264 if (is_changed) 264 if (is_changed)
265 { 265 {
266 Ac = A; 266 Ac = A;
267 Ac = Ac.single_subsref ("(", idx); 267 Ac = Ac.single_subsref ("(", idx);
268 return true; 268 return true;
269 } 269 }
270 else 270 else
271 return false; 271 return false;
272 } 272 }
273 } 273 }
274 274
275 #if 0 275 #if 0
276 // FIXME -- this function is not used; is it OK to delete it? 276 // FIXME -- this function is not used; is it OK to delete it?
280 octave_idx_type nd = dv.length (); 280 octave_idx_type nd = dv.length ();
281 281
282 if (i == 0) 282 if (i == 0)
283 { 283 {
284 for (octave_idx_type j = nd - 1; j > 0; j--) 284 for (octave_idx_type j = nd - 1; j > 0; j--)
285 idx(j) = octave_value (static_cast<double>(1)); 285 idx(j) = octave_value (static_cast<double>(1));
286 idx(0) = octave_value (':'); 286 idx(0) = octave_value (':');
287 } 287 }
288 else 288 else
289 { 289 {
290 for (octave_idx_type j = 1; j < nd; j++) 290 for (octave_idx_type j = 1; j < nd; j++)
291 { 291 {
292 idx (j) = octave_value (i % dv (j) + 1); 292 idx (j) = octave_value (i % dv (j) + 1);
293 i = i / dv (j); 293 i = i / dv (j);
294 } 294 }
295 } 295 }
296 } 296 }
297 #endif 297 #endif
298 298
299 static void 299 static void
359 retval(0) = tmp; 359 retval(0) = tmp;
360 } 360 }
361 } 361 }
362 362
363 if (! error_state && retval.empty ()) 363 if (! error_state && retval.empty ())
364 { 364 {
365 dim_vector dva = A.dims (); 365 dim_vector dva = A.dims ();
366 octave_idx_type nda = dva.length (); 366 octave_idx_type nda = dva.length ();
367 dim_vector dvb = B.dims (); 367 dim_vector dvb = B.dims ();
368 octave_idx_type ndb = dvb.length (); 368 octave_idx_type ndb = dvb.length ();
369 octave_idx_type nd = nda; 369 octave_idx_type nd = nda;
370 370
371 if (nda > ndb) 371 if (nda > ndb)
372 dvb.resize (nda, 1); 372 dvb.resize (nda, 1);
373 else if (nda < ndb) 373 else if (nda < ndb)
374 { 374 {
375 dva.resize (ndb, 1); 375 dva.resize (ndb, 1);
376 nd = ndb; 376 nd = ndb;
377 } 377 }
378 378
379 for (octave_idx_type i = 0; i < nd; i++) 379 for (octave_idx_type i = 0; i < nd; i++)
380 if (dva (i) != dvb (i) && dva (i) != 1 && dvb (i) != 1) 380 if (dva (i) != dvb (i) && dva (i) != 1 && dvb (i) != 1)
381 { 381 {
382 error ("bsxfun: dimensions don't match"); 382 error ("bsxfun: dimensions don't match");
383 break; 383 break;
384 } 384 }
385 385
386 if (!error_state) 386 if (!error_state)
387 { 387 {
388 // Find the size of the output 388 // Find the size of the output
389 dim_vector dvc; 389 dim_vector dvc;
390 dvc.resize (nd); 390 dvc.resize (nd);
391 391
392 for (octave_idx_type i = 0; i < nd; i++) 392 for (octave_idx_type i = 0; i < nd; i++)
393 dvc (i) = (dva (i) < 1 ? dva (i) : (dvb (i) < 1 ? dvb (i) : 393 dvc (i) = (dva (i) < 1 ? dva (i) : (dvb (i) < 1 ? dvb (i) :
394 (dva (i) > dvb (i) ? dva (i) : dvb (i)))); 394 (dva (i) > dvb (i) ? dva (i) : dvb (i))));
395 395
396 if (dva == dvb || dva.numel () == 1 || dvb.numel () == 1) 396 if (dva == dvb || dva.numel () == 1 || dvb.numel () == 1)
397 { 397 {
398 octave_value_list inputs; 398 octave_value_list inputs;
399 inputs (0) = A; 399 inputs (0) = A;
400 inputs (1) = B; 400 inputs (1) = B;
401 retval = func.do_multi_index_op (1, inputs); 401 retval = func.do_multi_index_op (1, inputs);
402 } 402 }
403 else if (dvc.numel () < 1) 403 else if (dvc.numel () < 1)
404 { 404 {
405 octave_value_list inputs; 405 octave_value_list inputs;
406 inputs (0) = A.resize (dvc); 406 inputs (0) = A.resize (dvc);
407 inputs (1) = B.resize (dvc); 407 inputs (1) = B.resize (dvc);
408 retval = func.do_multi_index_op (1, inputs); 408 retval = func.do_multi_index_op (1, inputs);
409 } 409 }
410 else 410 else
411 { 411 {
412 octave_idx_type ncount = 1; 412 octave_idx_type ncount = 1;
413 for (octave_idx_type i = 1; i < nd; i++) 413 for (octave_idx_type i = 1; i < nd; i++)
414 ncount *= dvc (i); 414 ncount *= dvc (i);
415 415
416 #define BSXDEF(T) \ 416 #define BSXDEF(T) \
417 T result_ ## T; \ 417 T result_ ## T; \
418 bool have_ ## T = false; 418 bool have_ ## T = false;
419 419
420 BSXDEF(NDArray); 420 BSXDEF(NDArray);
421 BSXDEF(ComplexNDArray); 421 BSXDEF(ComplexNDArray);
422 BSXDEF(FloatNDArray); 422 BSXDEF(FloatNDArray);
423 BSXDEF(FloatComplexNDArray); 423 BSXDEF(FloatComplexNDArray);
424 BSXDEF(boolNDArray); 424 BSXDEF(boolNDArray);
425 BSXDEF(int8NDArray); 425 BSXDEF(int8NDArray);
426 BSXDEF(int16NDArray); 426 BSXDEF(int16NDArray);
427 BSXDEF(int32NDArray); 427 BSXDEF(int32NDArray);
428 BSXDEF(int64NDArray); 428 BSXDEF(int64NDArray);
429 BSXDEF(uint8NDArray); 429 BSXDEF(uint8NDArray);
430 BSXDEF(uint16NDArray); 430 BSXDEF(uint16NDArray);
431 BSXDEF(uint32NDArray); 431 BSXDEF(uint32NDArray);
432 BSXDEF(uint64NDArray); 432 BSXDEF(uint64NDArray);
433 433
434 octave_value Ac ; 434 octave_value Ac ;
435 octave_value_list idxA; 435 octave_value_list idxA;
436 octave_value Bc; 436 octave_value Bc;
437 octave_value_list idxB; 437 octave_value_list idxB;
438 octave_value C; 438 octave_value C;
439 octave_value_list inputs; 439 octave_value_list inputs;
440 Array<int> ra_idx (dvc.length(), 0); 440 Array<int> ra_idx (dvc.length(), 0);
441 441
442 442
443 for (octave_idx_type i = 0; i < ncount; i++) 443 for (octave_idx_type i = 0; i < ncount; i++)
444 { 444 {
445 if (maybe_update_column (Ac, A, dva, dvc, i, idxA)) 445 if (maybe_update_column (Ac, A, dva, dvc, i, idxA))
446 inputs (0) = Ac; 446 inputs (0) = Ac;
447 447
448 if (maybe_update_column (Bc, B, dvb, dvc, i, idxB)) 448 if (maybe_update_column (Bc, B, dvb, dvc, i, idxB))
449 inputs (1) = Bc; 449 inputs (1) = Bc;
450 450
451 octave_value_list tmp = func.do_multi_index_op (1, inputs); 451 octave_value_list tmp = func.do_multi_index_op (1, inputs);
452 452
453 if (error_state) 453 if (error_state)
454 break; 454 break;
455 455
456 #define BSXINIT(T, CLS, EXTRACTOR) \ 456 #define BSXINIT(T, CLS, EXTRACTOR) \
457 (result_type == CLS) \ 457 (result_type == CLS) \
458 { \ 458 { \
459 have_ ## T = true; \ 459 have_ ## T = true; \
460 result_ ## T = \ 460 result_ ## T = \
461 tmp (0). EXTRACTOR ## _array_value (); \ 461 tmp (0). EXTRACTOR ## _array_value (); \
462 result_ ## T .resize (dvc); \ 462 result_ ## T .resize (dvc); \
463 } 463 }
464 464
465 if (i == 0) 465 if (i == 0)
466 { 466 {
467 if (! tmp(0).is_sparse_type ()) 467 if (! tmp(0).is_sparse_type ())
468 { 468 {
469 std::string result_type = tmp(0).class_name (); 469 std::string result_type = tmp(0).class_name ();
470 if (result_type == "double") 470 if (result_type == "double")
471 { 471 {
472 if (tmp(0).is_real_type ()) 472 if (tmp(0).is_real_type ())
473 { 473 {
474 have_NDArray = true; 474 have_NDArray = true;
475 result_NDArray = tmp(0).array_value (); 475 result_NDArray = tmp(0).array_value ();
476 result_NDArray.resize (dvc); 476 result_NDArray.resize (dvc);
477 } 477 }
478 else 478 else
479 { 479 {
480 have_ComplexNDArray = true; 480 have_ComplexNDArray = true;
481 result_ComplexNDArray = 481 result_ComplexNDArray =
482 tmp(0).complex_array_value (); 482 tmp(0).complex_array_value ();
483 result_ComplexNDArray.resize (dvc); 483 result_ComplexNDArray.resize (dvc);
484 } 484 }
485 } 485 }
486 else if (result_type == "single") 486 else if (result_type == "single")
487 { 487 {
488 if (tmp(0).is_real_type ()) 488 if (tmp(0).is_real_type ())
489 { 489 {
490 have_FloatNDArray = true; 490 have_FloatNDArray = true;
491 result_FloatNDArray = tmp(0).float_array_value (); 491 result_FloatNDArray = tmp(0).float_array_value ();
492 result_FloatNDArray.resize (dvc); 492 result_FloatNDArray.resize (dvc);
493 } 493 }
494 else 494 else
495 { 495 {
496 have_ComplexNDArray = true; 496 have_ComplexNDArray = true;
497 result_ComplexNDArray = 497 result_ComplexNDArray =
498 tmp(0).complex_array_value (); 498 tmp(0).complex_array_value ();
499 result_ComplexNDArray.resize (dvc); 499 result_ComplexNDArray.resize (dvc);
500 } 500 }
501 } 501 }
502 else if BSXINIT(boolNDArray, "logical", bool) 502 else if BSXINIT(boolNDArray, "logical", bool)
503 else if BSXINIT(int8NDArray, "int8", int8) 503 else if BSXINIT(int8NDArray, "int8", int8)
504 else if BSXINIT(int16NDArray, "int16", int16) 504 else if BSXINIT(int16NDArray, "int16", int16)
505 else if BSXINIT(int32NDArray, "int32", int32) 505 else if BSXINIT(int32NDArray, "int32", int32)
506 else if BSXINIT(int64NDArray, "int64", int64) 506 else if BSXINIT(int64NDArray, "int64", int64)
507 else if BSXINIT(uint8NDArray, "uint8", uint8) 507 else if BSXINIT(uint8NDArray, "uint8", uint8)
508 else if BSXINIT(uint16NDArray, "uint16", uint16) 508 else if BSXINIT(uint16NDArray, "uint16", uint16)
509 else if BSXINIT(uint32NDArray, "uint32", uint32) 509 else if BSXINIT(uint32NDArray, "uint32", uint32)
510 else if BSXINIT(uint64NDArray, "uint64", uint64) 510 else if BSXINIT(uint64NDArray, "uint64", uint64)
511 else 511 else
512 { 512 {
513 C = tmp (0); 513 C = tmp (0);
514 C = C.resize (dvc); 514 C = C.resize (dvc);
515 } 515 }
516 } 516 }
517 } 517 }
518 else 518 else
519 { 519 {
520 update_index (ra_idx, dvc, i); 520 update_index (ra_idx, dvc, i);
521 521
522 if (have_FloatNDArray || 522 if (have_FloatNDArray ||
523 have_FloatComplexNDArray) 523 have_FloatComplexNDArray)
524 { 524 {
525 if (! tmp(0).is_float_type ()) 525 if (! tmp(0).is_float_type ())
526 { 526 {
527 if (have_FloatNDArray) 527 if (have_FloatNDArray)
528 { 528 {
529 have_FloatNDArray = false; 529 have_FloatNDArray = false;
530 C = result_FloatNDArray; 530 C = result_FloatNDArray;
531 } 531 }
532 else 532 else
533 { 533 {
534 have_FloatComplexNDArray = false; 534 have_FloatComplexNDArray = false;
535 C = result_FloatComplexNDArray; 535 C = result_FloatComplexNDArray;
536 } 536 }
537 C = do_cat_op (C, tmp(0), ra_idx); 537 C = do_cat_op (C, tmp(0), ra_idx);
538 } 538 }
539 else if (tmp(0).is_double_type ()) 539 else if (tmp(0).is_double_type ())
540 { 540 {
541 if (tmp(0).is_complex_type () && 541 if (tmp(0).is_complex_type () &&
542 have_FloatNDArray) 542 have_FloatNDArray)
543 { 543 {
544 result_ComplexNDArray = 544 result_ComplexNDArray =
545 ComplexNDArray (result_FloatNDArray); 545 ComplexNDArray (result_FloatNDArray);
546 result_ComplexNDArray.insert 546 result_ComplexNDArray.insert
547 (tmp(0).complex_array_value(), ra_idx); 547 (tmp(0).complex_array_value(), ra_idx);
548 have_FloatComplexNDArray = false; 548 have_FloatComplexNDArray = false;
549 have_ComplexNDArray = true; 549 have_ComplexNDArray = true;
550 } 550 }
551 else 551 else
552 { 552 {
553 result_NDArray = 553 result_NDArray =
554 NDArray (result_FloatNDArray); 554 NDArray (result_FloatNDArray);
555 result_NDArray.insert 555 result_NDArray.insert
556 (tmp(0).array_value(), ra_idx); 556 (tmp(0).array_value(), ra_idx);
557 have_FloatNDArray = false; 557 have_FloatNDArray = false;
558 have_NDArray = true; 558 have_NDArray = true;
559 } 559 }
560 } 560 }
561 else if (tmp(0).is_real_type ()) 561 else if (tmp(0).is_real_type ())
562 result_FloatNDArray.insert 562 result_FloatNDArray.insert
563 (tmp(0).float_array_value(), ra_idx); 563 (tmp(0).float_array_value(), ra_idx);
564 else 564 else
565 { 565 {
566 result_FloatComplexNDArray = 566 result_FloatComplexNDArray =
567 FloatComplexNDArray (result_FloatNDArray); 567 FloatComplexNDArray (result_FloatNDArray);
568 result_FloatComplexNDArray.insert 568 result_FloatComplexNDArray.insert
569 (tmp(0).float_complex_array_value(), ra_idx); 569 (tmp(0).float_complex_array_value(), ra_idx);
570 have_FloatNDArray = false; 570 have_FloatNDArray = false;
571 have_FloatComplexNDArray = true; 571 have_FloatComplexNDArray = true;
572 } 572 }
573 } 573 }
574 else if (have_NDArray) 574 else if (have_NDArray)
575 { 575 {
576 if (! tmp(0).is_float_type ()) 576 if (! tmp(0).is_float_type ())
577 { 577 {
578 have_NDArray = false; 578 have_NDArray = false;
579 C = result_NDArray; 579 C = result_NDArray;
580 C = do_cat_op (C, tmp(0), ra_idx); 580 C = do_cat_op (C, tmp(0), ra_idx);
581 } 581 }
582 else if (tmp(0).is_real_type ()) 582 else if (tmp(0).is_real_type ())
583 result_NDArray.insert (tmp(0).array_value(), 583 result_NDArray.insert (tmp(0).array_value(),
584 ra_idx); 584 ra_idx);
585 else 585 else
586 { 586 {
587 result_ComplexNDArray = 587 result_ComplexNDArray =
588 ComplexNDArray (result_NDArray); 588 ComplexNDArray (result_NDArray);
589 result_ComplexNDArray.insert 589 result_ComplexNDArray.insert
590 (tmp(0).complex_array_value(), ra_idx); 590 (tmp(0).complex_array_value(), ra_idx);
591 have_NDArray = false; 591 have_NDArray = false;
592 have_ComplexNDArray = true; 592 have_ComplexNDArray = true;
593 } 593 }
594 } 594 }
595 595
596 #define BSXLOOP(T, CLS, EXTRACTOR) \ 596 #define BSXLOOP(T, CLS, EXTRACTOR) \
597 (have_ ## T) \ 597 (have_ ## T) \
598 { \ 598 { \
599 if (tmp (0).class_name () != CLS) \ 599 if (tmp (0).class_name () != CLS) \
600 { \ 600 { \
601 have_ ## T = false; \ 601 have_ ## T = false; \
602 C = result_ ## T; \ 602 C = result_ ## T; \
603 C = do_cat_op (C, tmp (0), ra_idx); \ 603 C = do_cat_op (C, tmp (0), ra_idx); \
604 } \ 604 } \
605 else \ 605 else \
606 result_ ## T .insert \ 606 result_ ## T .insert \
607 (tmp(0). EXTRACTOR ## _array_value (), \ 607 (tmp(0). EXTRACTOR ## _array_value (), \
608 ra_idx); \ 608 ra_idx); \
609 } 609 }
610 610
611 else if BSXLOOP(ComplexNDArray, "double", complex) 611 else if BSXLOOP(ComplexNDArray, "double", complex)
612 else if BSXLOOP(boolNDArray, "logical", bool) 612 else if BSXLOOP(boolNDArray, "logical", bool)
613 else if BSXLOOP(int8NDArray, "int8", int8) 613 else if BSXLOOP(int8NDArray, "int8", int8)
614 else if BSXLOOP(int16NDArray, "int16", int16) 614 else if BSXLOOP(int16NDArray, "int16", int16)
615 else if BSXLOOP(int32NDArray, "int32", int32) 615 else if BSXLOOP(int32NDArray, "int32", int32)
616 else if BSXLOOP(int64NDArray, "int64", int64) 616 else if BSXLOOP(int64NDArray, "int64", int64)
617 else if BSXLOOP(uint8NDArray, "uint8", uint8) 617 else if BSXLOOP(uint8NDArray, "uint8", uint8)
618 else if BSXLOOP(uint16NDArray, "uint16", uint16) 618 else if BSXLOOP(uint16NDArray, "uint16", uint16)
619 else if BSXLOOP(uint32NDArray, "uint32", uint32) 619 else if BSXLOOP(uint32NDArray, "uint32", uint32)
620 else if BSXLOOP(uint64NDArray, "uint64", uint64) 620 else if BSXLOOP(uint64NDArray, "uint64", uint64)
621 else 621 else
622 C = do_cat_op (C, tmp(0), ra_idx); 622 C = do_cat_op (C, tmp(0), ra_idx);
623 } 623 }
624 } 624 }
625 625
626 #define BSXEND(T) \ 626 #define BSXEND(T) \
627 (have_ ## T) \ 627 (have_ ## T) \
628 retval (0) = result_ ## T; 628 retval (0) = result_ ## T;
629 629
630 if BSXEND(NDArray) 630 if BSXEND(NDArray)
631 else if BSXEND(ComplexNDArray) 631 else if BSXEND(ComplexNDArray)
632 else if BSXEND(FloatNDArray) 632 else if BSXEND(FloatNDArray)
633 else if BSXEND(FloatComplexNDArray) 633 else if BSXEND(FloatComplexNDArray)
634 else if BSXEND(boolNDArray) 634 else if BSXEND(boolNDArray)
635 else if BSXEND(int8NDArray) 635 else if BSXEND(int8NDArray)
636 else if BSXEND(int16NDArray) 636 else if BSXEND(int16NDArray)
637 else if BSXEND(int32NDArray) 637 else if BSXEND(int32NDArray)
638 else if BSXEND(int64NDArray) 638 else if BSXEND(int64NDArray)
639 else if BSXEND(uint8NDArray) 639 else if BSXEND(uint8NDArray)
640 else if BSXEND(uint16NDArray) 640 else if BSXEND(uint16NDArray)
641 else if BSXEND(uint32NDArray) 641 else if BSXEND(uint32NDArray)
642 else if BSXEND(uint64NDArray) 642 else if BSXEND(uint64NDArray)
643 else 643 else
644 retval(0) = C; 644 retval(0) = C;
645 } 645 }
646 } 646 }
647 } 647 }
648 } 648 }
649 649
650 return retval; 650 return retval;
651 } 651 }
652 652
653 /* 653 /*