comparison src/ov.cc @ 7892:7ca2735d74c2

simplify & cleanup octave_value::XXX_vector_value functions
author Jaroslav Hajek <highegg@gmail.com>
date Mon, 26 May 2008 14:01:55 +0200
parents f336dd8e96d0
children 2b8952e133c9
comparison
equal deleted inserted replaced
7891:0280a546622c 7892:7ca2735d74c2
1279 octave_value::list_value (void) const 1279 octave_value::list_value (void) const
1280 { 1280 {
1281 return rep->list_value (); 1281 return rep->list_value ();
1282 } 1282 }
1283 1283
1284 static dim_vector
1285 make_vector_dims (const dim_vector& dv, bool force_vector_conversion,
1286 const std::string& my_type, const std::string& wanted_type)
1287 {
1288 dim_vector retval (dv);
1289 retval.chop_trailing_singletons ();
1290 octave_idx_type nel = dv.numel ();
1291
1292 if (retval.length () > 2 || (retval(0) != 1 && retval(1) != 1))
1293 {
1294 if (!force_vector_conversion)
1295 gripe_implicit_conversion ("Octave:array-as-vector",
1296 my_type.c_str (), wanted_type.c_str ());
1297 retval = dim_vector (nel);
1298 }
1299
1300 return retval;
1301 }
1302
1284 ColumnVector 1303 ColumnVector
1285 octave_value::column_vector_value (bool force_string_conv, 1304 octave_value::column_vector_value (bool force_string_conv,
1286 bool /* frc_vec_conv */) const 1305 bool frc_vec_conv) const
1287 { 1306 {
1288 ColumnVector retval; 1307 return ColumnVector (vector_value (force_string_conv,
1289 1308 frc_vec_conv));
1290 Matrix m = matrix_value (force_string_conv);
1291
1292 if (error_state)
1293 return retval;
1294
1295 octave_idx_type nr = m.rows ();
1296 octave_idx_type nc = m.columns ();
1297
1298 if (nc == 1)
1299 {
1300 retval.resize (nr);
1301 for (octave_idx_type i = 0; i < nr; i++)
1302 retval (i) = m (i, 0);
1303 }
1304 else
1305 {
1306 std::string tn = type_name ();
1307 gripe_invalid_conversion (tn.c_str (), "real column vector");
1308 }
1309
1310 return retval;
1311 } 1309 }
1312 1310
1313 ComplexColumnVector 1311 ComplexColumnVector
1314 octave_value::complex_column_vector_value (bool force_string_conv, 1312 octave_value::complex_column_vector_value (bool force_string_conv,
1315 bool /* frc_vec_conv */) const 1313 bool frc_vec_conv) const
1316 { 1314 {
1317 ComplexColumnVector retval; 1315 return ComplexColumnVector (complex_vector_value (force_string_conv,
1318 1316 frc_vec_conv));
1319 ComplexMatrix m = complex_matrix_value (force_string_conv);
1320
1321 if (error_state)
1322 return retval;
1323
1324 octave_idx_type nr = m.rows ();
1325 octave_idx_type nc = m.columns ();
1326
1327 if (nc == 1)
1328 {
1329 retval.resize (nr);
1330 for (octave_idx_type i = 0; i < nr; i++)
1331 retval (i) = m (i, 0);
1332 }
1333 else
1334 {
1335 std::string tn = type_name ();
1336 gripe_invalid_conversion (tn.c_str (), "complex column vector");
1337 }
1338
1339 return retval;
1340 } 1317 }
1341 1318
1342 RowVector 1319 RowVector
1343 octave_value::row_vector_value (bool force_string_conv, 1320 octave_value::row_vector_value (bool force_string_conv,
1344 bool /* frc_vec_conv */) const 1321 bool frc_vec_conv) const
1345 { 1322 {
1346 RowVector retval; 1323 return RowVector (vector_value (force_string_conv,
1347 1324 frc_vec_conv));
1348 Matrix m = matrix_value (force_string_conv);
1349
1350 if (error_state)
1351 return retval;
1352
1353 octave_idx_type nr = m.rows ();
1354 octave_idx_type nc = m.columns ();
1355
1356 if (nr == 1)
1357 {
1358 retval.resize (nc);
1359 for (octave_idx_type i = 0; i < nc; i++)
1360 retval (i) = m (0, i);
1361 }
1362 else
1363 {
1364 std::string tn = type_name ();
1365 gripe_invalid_conversion (tn.c_str (), "real row vector");
1366 }
1367
1368 return retval;
1369 } 1325 }
1370 1326
1371 ComplexRowVector 1327 ComplexRowVector
1372 octave_value::complex_row_vector_value (bool force_string_conv, 1328 octave_value::complex_row_vector_value (bool force_string_conv,
1373 bool /* frc_vec_conv */) const 1329 bool frc_vec_conv) const
1374 { 1330 {
1375 ComplexRowVector retval; 1331 return ComplexRowVector (complex_vector_value (force_string_conv,
1376 1332 frc_vec_conv));
1377 ComplexMatrix m = complex_matrix_value (force_string_conv); 1333 }
1378
1379 if (error_state)
1380 return retval;
1381
1382 octave_idx_type nr = m.rows ();
1383 octave_idx_type nc = m.columns ();
1384
1385 if (nr == 1)
1386 {
1387 retval.resize (nc);
1388 for (octave_idx_type i = 0; i < nc; i++)
1389 retval (i) = m (0, i);
1390 }
1391 else
1392 {
1393 std::string tn = type_name ();
1394 gripe_invalid_conversion (tn.c_str (), "complex row vector");
1395 }
1396
1397 return retval;
1398 }
1399
1400 // Sloppy...
1401 1334
1402 Array<double> 1335 Array<double>
1403 octave_value::vector_value (bool force_string_conv, 1336 octave_value::vector_value (bool force_string_conv,
1404 bool force_vector_conversion) const 1337 bool force_vector_conversion) const
1405 { 1338 {
1406 Array<double> retval; 1339 Array<double> retval = array_value (force_string_conv);
1407
1408 Matrix m = matrix_value (force_string_conv);
1409 1340
1410 if (error_state) 1341 if (error_state)
1411 return retval; 1342 return retval;
1412
1413 octave_idx_type nr = m.rows ();
1414 octave_idx_type nc = m.columns ();
1415
1416 if (nr == 1)
1417 {
1418 retval.resize (nc);
1419 for (octave_idx_type i = 0; i < nc; i++)
1420 retval (i) = m (0, i);
1421 }
1422 else if (nc == 1)
1423 {
1424 retval.resize (nr);
1425 for (octave_idx_type i = 0; i < nr; i++)
1426 retval (i) = m (i, 0);
1427 }
1428 else if (nr > 0 && nc > 0)
1429 {
1430 if (! force_vector_conversion)
1431 gripe_implicit_conversion ("Octave:array-as-vector",
1432 type_name (), "real vector");
1433
1434 retval.resize (nr * nc);
1435 octave_idx_type k = 0;
1436 for (octave_idx_type j = 0; j < nc; j++)
1437 for (octave_idx_type i = 0; i < nr; i++)
1438 {
1439 OCTAVE_QUIT;
1440
1441 retval (k++) = m (i, j);
1442 }
1443 }
1444 else 1343 else
1445 { 1344 return retval.reshape (make_vector_dims (retval.dims (),
1446 std::string tn = type_name (); 1345 force_vector_conversion,
1447 gripe_invalid_conversion (tn.c_str (), "real vector"); 1346 type_name (), "real vector"));
1448 }
1449
1450 return retval;
1451 } 1347 }
1452 1348
1453 Array<int> 1349 Array<int>
1454 octave_value::int_vector_value (bool force_string_conv, bool require_int, 1350 octave_value::int_vector_value (bool force_string_conv, bool require_int,
1455 bool force_vector_conversion) const 1351 bool force_vector_conversion) const
1456 { 1352 {
1457 Array<int> retval; 1353 Array<int> retval;
1458 1354
1459 Matrix m = matrix_value (force_string_conv); 1355 if (is_integer_type ())
1356 {
1357 // query for the first type that is wide enough
1358 #if SIZEOF_INT == 2
1359 retval = int16_array_value ();
1360 #elif SIZEOF_INT == 4
1361 retval = int32_array_value ();
1362 #else
1363 retval = int64_array_value ();
1364 #endif
1365 }
1366 else
1367 {
1368 const NDArray a = array_value (force_string_conv);
1369 if (! error_state)
1370 {
1371 if (require_int)
1372 {
1373 retval.resize (a.dims ());
1374 for (octave_idx_type i = 0; i < a.numel (); i++)
1375 {
1376 double ai = a.elem (i);
1377 int v = static_cast<int> (ai);
1378 if (ai == v)
1379 retval.xelem (i) = v;
1380 else
1381 {
1382 error ("conversion to integer value failed");
1383 break;
1384 }
1385 }
1386 }
1387 else
1388 retval = Array<int> (a);
1389 }
1390 }
1391
1460 1392
1461 if (error_state) 1393 if (error_state)
1462 return retval; 1394 return retval;
1463
1464 octave_idx_type nr = m.rows ();
1465 octave_idx_type nc = m.columns ();
1466
1467 if (nr == 1)
1468 {
1469 retval.resize (nc);
1470 for (octave_idx_type i = 0; i < nc; i++)
1471 {
1472 OCTAVE_QUIT;
1473
1474 double d = m (0, i);
1475
1476 if (require_int && D_NINT (d) != d)
1477 {
1478 error ("conversion to integer value failed");
1479 return retval;
1480 }
1481
1482 retval (i) = static_cast<int> (d);
1483 }
1484 }
1485 else if (nc == 1)
1486 {
1487 retval.resize (nr);
1488 for (octave_idx_type i = 0; i < nr; i++)
1489 {
1490 OCTAVE_QUIT;
1491
1492 double d = m (i, 0);
1493
1494 if (require_int && D_NINT (d) != d)
1495 {
1496 error ("conversion to integer value failed");
1497 return retval;
1498 }
1499
1500 retval (i) = static_cast<int> (d);
1501 }
1502 }
1503 else if (nr > 0 && nc > 0)
1504 {
1505 if (! force_vector_conversion)
1506 gripe_implicit_conversion ("Octave:array-as-vector",
1507 type_name (), "real vector");
1508
1509 retval.resize (nr * nc);
1510 octave_idx_type k = 0;
1511 for (octave_idx_type j = 0; j < nc; j++)
1512 {
1513 for (octave_idx_type i = 0; i < nr; i++)
1514 {
1515 OCTAVE_QUIT;
1516
1517 double d = m (i, j);
1518
1519 if (require_int && D_NINT (d) != d)
1520 {
1521 error ("conversion to integer value failed");
1522 return retval;
1523 }
1524
1525 retval (k++) = static_cast<int> (d);
1526 }
1527 }
1528 }
1529 else 1395 else
1530 { 1396 return retval.reshape (make_vector_dims (retval.dims (),
1531 std::string tn = type_name (); 1397 force_vector_conversion,
1532 gripe_invalid_conversion (tn.c_str (), "real vector"); 1398 type_name (), "integer vector"));
1533 }
1534
1535 return retval;
1536 } 1399 }
1537 1400
1538 Array<Complex> 1401 Array<Complex>
1539 octave_value::complex_vector_value (bool force_string_conv, 1402 octave_value::complex_vector_value (bool force_string_conv,
1540 bool force_vector_conversion) const 1403 bool force_vector_conversion) const
1541 { 1404 {
1542 Array<Complex> retval; 1405 Array<Complex> retval = complex_array_value (force_string_conv);
1543
1544 ComplexMatrix m = complex_matrix_value (force_string_conv);
1545 1406
1546 if (error_state) 1407 if (error_state)
1547 return retval; 1408 return retval;
1548
1549 octave_idx_type nr = m.rows ();
1550 octave_idx_type nc = m.columns ();
1551
1552 if (nr == 1)
1553 {
1554 retval.resize (nc);
1555 for (octave_idx_type i = 0; i < nc; i++)
1556 {
1557 OCTAVE_QUIT;
1558 retval (i) = m (0, i);
1559 }
1560 }
1561 else if (nc == 1)
1562 {
1563 retval.resize (nr);
1564 for (octave_idx_type i = 0; i < nr; i++)
1565 {
1566 OCTAVE_QUIT;
1567 retval (i) = m (i, 0);
1568 }
1569 }
1570 else if (nr > 0 && nc > 0)
1571 {
1572 if (! force_vector_conversion)
1573 gripe_implicit_conversion ("Octave:array-as-vector",
1574 type_name (), "complex vector");
1575
1576 retval.resize (nr * nc);
1577 octave_idx_type k = 0;
1578 for (octave_idx_type j = 0; j < nc; j++)
1579 for (octave_idx_type i = 0; i < nr; i++)
1580 {
1581 OCTAVE_QUIT;
1582 retval (k++) = m (i, j);
1583 }
1584 }
1585 else 1409 else
1586 { 1410 return retval.reshape (make_vector_dims (retval.dims (),
1587 std::string tn = type_name (); 1411 force_vector_conversion,
1588 gripe_invalid_conversion (tn.c_str (), "complex vector"); 1412 type_name (), "complex vector"));
1589 }
1590
1591 return retval;
1592 } 1413 }
1593 1414
1594 FloatColumnVector 1415 FloatColumnVector
1595 octave_value::float_column_vector_value (bool force_string_conv, 1416 octave_value::float_column_vector_value (bool force_string_conv,
1596 bool /* frc_vec_conv */) const 1417 bool frc_vec_conv) const
1597 { 1418 {
1598 FloatColumnVector retval; 1419 return FloatColumnVector (float_vector_value (force_string_conv,
1599 1420 frc_vec_conv));
1600 FloatMatrix m = float_matrix_value (force_string_conv); 1421 }
1422
1423 FloatComplexColumnVector
1424 octave_value::float_complex_column_vector_value (bool force_string_conv,
1425 bool frc_vec_conv) const
1426 {
1427 return FloatComplexColumnVector (float_complex_vector_value (force_string_conv,
1428 frc_vec_conv));
1429 }
1430
1431 FloatRowVector
1432 octave_value::float_row_vector_value (bool force_string_conv,
1433 bool frc_vec_conv) const
1434 {
1435 return FloatRowVector (float_vector_value (force_string_conv,
1436 frc_vec_conv));
1437 }
1438
1439 FloatComplexRowVector
1440 octave_value::float_complex_row_vector_value (bool force_string_conv,
1441 bool frc_vec_conv) const
1442 {
1443 return FloatComplexRowVector (float_complex_vector_value (force_string_conv,
1444 frc_vec_conv));
1445 }
1446
1447 Array<float>
1448 octave_value::float_vector_value (bool force_string_conv,
1449 bool force_vector_conversion) const
1450 {
1451 Array<float> retval = float_array_value (force_string_conv);
1601 1452
1602 if (error_state) 1453 if (error_state)
1603 return retval; 1454 return retval;
1604
1605 octave_idx_type nr = m.rows ();
1606 octave_idx_type nc = m.columns ();
1607
1608 if (nc == 1)
1609 {
1610 retval.resize (nr);
1611 for (octave_idx_type i = 0; i < nr; i++)
1612 retval (i) = m (i, 0);
1613 }
1614 else 1455 else
1615 { 1456 return retval.reshape (make_vector_dims (retval.dims (),
1616 std::string tn = type_name (); 1457 force_vector_conversion,
1617 gripe_invalid_conversion (tn.c_str (), "real column vector"); 1458 type_name (), "real vector"));
1618 } 1459 }
1619 1460
1620 return retval; 1461 Array<FloatComplex>
1621 } 1462 octave_value::float_complex_vector_value (bool force_string_conv,
1622 1463 bool force_vector_conversion) const
1623 FloatComplexColumnVector 1464 {
1624 octave_value::float_complex_column_vector_value (bool force_string_conv, 1465 Array<FloatComplex> retval = float_complex_array_value (force_string_conv);
1625 bool /* frc_vec_conv */) const
1626 {
1627 FloatComplexColumnVector retval;
1628
1629 FloatComplexMatrix m = float_complex_matrix_value (force_string_conv);
1630 1466
1631 if (error_state) 1467 if (error_state)
1632 return retval; 1468 return retval;
1633
1634 octave_idx_type nr = m.rows ();
1635 octave_idx_type nc = m.columns ();
1636
1637 if (nc == 1)
1638 {
1639 retval.resize (nr);
1640 for (octave_idx_type i = 0; i < nr; i++)
1641 retval (i) = m (i, 0);
1642 }
1643 else 1469 else
1644 { 1470 return retval.reshape (make_vector_dims (retval.dims (),
1645 std::string tn = type_name (); 1471 force_vector_conversion,
1646 gripe_invalid_conversion (tn.c_str (), "complex column vector"); 1472 type_name (), "complex vector"));
1647 }
1648
1649 return retval;
1650 }
1651
1652 FloatRowVector
1653 octave_value::float_row_vector_value (bool force_string_conv,
1654 bool /* frc_vec_conv */) const
1655 {
1656 FloatRowVector retval;
1657
1658 FloatMatrix m = float_matrix_value (force_string_conv);
1659
1660 if (error_state)
1661 return retval;
1662
1663 octave_idx_type nr = m.rows ();
1664 octave_idx_type nc = m.columns ();
1665
1666 if (nr == 1)
1667 {
1668 retval.resize (nc);
1669 for (octave_idx_type i = 0; i < nc; i++)
1670 retval (i) = m (0, i);
1671 }
1672 else
1673 {
1674 std::string tn = type_name ();
1675 gripe_invalid_conversion (tn.c_str (), "real row vector");
1676 }
1677
1678 return retval;
1679 }
1680
1681 FloatComplexRowVector
1682 octave_value::float_complex_row_vector_value (bool force_string_conv,
1683 bool /* frc_vec_conv */) const
1684 {
1685 FloatComplexRowVector retval;
1686
1687 FloatComplexMatrix m = float_complex_matrix_value (force_string_conv);
1688
1689 if (error_state)
1690 return retval;
1691
1692 octave_idx_type nr = m.rows ();
1693 octave_idx_type nc = m.columns ();
1694
1695 if (nr == 1)
1696 {
1697 retval.resize (nc);
1698 for (octave_idx_type i = 0; i < nc; i++)
1699 retval (i) = m (0, i);
1700 }
1701 else
1702 {
1703 std::string tn = type_name ();
1704 gripe_invalid_conversion (tn.c_str (), "complex row vector");
1705 }
1706
1707 return retval;
1708 }
1709
1710 // Sloppy...
1711
1712 Array<float>
1713 octave_value::float_vector_value (bool force_string_conv,
1714 bool force_vector_conversion) const
1715 {
1716 Array<float> retval;
1717
1718 FloatMatrix m = float_matrix_value (force_string_conv);
1719
1720 if (error_state)
1721 return retval;
1722
1723 octave_idx_type nr = m.rows ();
1724 octave_idx_type nc = m.columns ();
1725
1726 if (nr == 1)
1727 {
1728 retval.resize (nc);
1729 for (octave_idx_type i = 0; i < nc; i++)
1730 retval (i) = m (0, i);
1731 }
1732 else if (nc == 1)
1733 {
1734 retval.resize (nr);
1735 for (octave_idx_type i = 0; i < nr; i++)
1736 retval (i) = m (i, 0);
1737 }
1738 else if (nr > 0 && nc > 0)
1739 {
1740 if (! force_vector_conversion)
1741 gripe_implicit_conversion ("Octave:array-as-vector",
1742 type_name (), "real vector");
1743
1744 retval.resize (nr * nc);
1745 octave_idx_type k = 0;
1746 for (octave_idx_type j = 0; j < nc; j++)
1747 for (octave_idx_type i = 0; i < nr; i++)
1748 {
1749 OCTAVE_QUIT;
1750
1751 retval (k++) = m (i, j);
1752 }
1753 }
1754 else
1755 {
1756 std::string tn = type_name ();
1757 gripe_invalid_conversion (tn.c_str (), "real vector");
1758 }
1759
1760 return retval;
1761 }
1762
1763 Array<FloatComplex>
1764 octave_value::float_complex_vector_value (bool force_string_conv,
1765 bool force_vector_conversion) const
1766 {
1767 Array<FloatComplex> retval;
1768
1769 FloatComplexMatrix m = float_complex_matrix_value (force_string_conv);
1770
1771 if (error_state)
1772 return retval;
1773
1774 octave_idx_type nr = m.rows ();
1775 octave_idx_type nc = m.columns ();
1776
1777 if (nr == 1)
1778 {
1779 retval.resize (nc);
1780 for (octave_idx_type i = 0; i < nc; i++)
1781 {
1782 OCTAVE_QUIT;
1783 retval (i) = m (0, i);
1784 }
1785 }
1786 else if (nc == 1)
1787 {
1788 retval.resize (nr);
1789 for (octave_idx_type i = 0; i < nr; i++)
1790 {
1791 OCTAVE_QUIT;
1792 retval (i) = m (i, 0);
1793 }
1794 }
1795 else if (nr > 0 && nc > 0)
1796 {
1797 if (! force_vector_conversion)
1798 gripe_implicit_conversion ("Octave:array-as-vector",
1799 type_name (), "complex vector");
1800
1801 retval.resize (nr * nc);
1802 octave_idx_type k = 0;
1803 for (octave_idx_type j = 0; j < nc; j++)
1804 for (octave_idx_type i = 0; i < nr; i++)
1805 {
1806 OCTAVE_QUIT;
1807 retval (k++) = m (i, j);
1808 }
1809 }
1810 else
1811 {
1812 std::string tn = type_name ();
1813 gripe_invalid_conversion (tn.c_str (), "complex vector");
1814 }
1815
1816 return retval;
1817 } 1473 }
1818 1474
1819 int 1475 int
1820 octave_value::write (octave_stream& os, int block_size, 1476 octave_value::write (octave_stream& os, int block_size,
1821 oct_data_conv::data_type output_type, int skip, 1477 oct_data_conv::data_type output_type, int skip,