comparison src/data.cc @ 6953:4567a35e0777

[project @ 2007-10-04 02:53:11 by jwe]
author jwe
date Thu, 04 Oct 2007 02:54:00 +0000
parents 6bbf56a9718a
children 47f4f4e88166
comparison
equal deleted inserted replaced
6952:09a89fb42c09 6953:4567a35e0777
32 32
33 #include "lo-ieee.h" 33 #include "lo-ieee.h"
34 #include "str-vec.h" 34 #include "str-vec.h"
35 #include "quit.h" 35 #include "quit.h"
36 36
37 #include "Cell.h"
37 #include "defun.h" 38 #include "defun.h"
38 #include "error.h" 39 #include "error.h"
39 #include "gripes.h" 40 #include "gripes.h"
41 #include "oct-map.h"
42 #include "oct-obj.h"
40 #include "ov.h" 43 #include "ov.h"
41 #include "ov-complex.h" 44 #include "ov-complex.h"
42 #include "ov-cx-mat.h" 45 #include "ov-cx-mat.h"
46 #include "parse.h"
47 #include "pt-mat.h"
48 #include "utils.h"
43 #include "variables.h" 49 #include "variables.h"
44 #include "oct-obj.h"
45 #include "utils.h"
46 #include "Cell.h"
47 #include "oct-map.h"
48 #include "pt-mat.h"
49 50
50 #define ANY_ALL(FCN) \ 51 #define ANY_ALL(FCN) \
51 \ 52 \
52 octave_value retval; \ 53 octave_value retval; \
53 \ 54 \
2612 print_usage (); 2613 print_usage ();
2613 2614
2614 return retval; 2615 return retval;
2615 } 2616 }
2616 2617
2618 /*
2619 %!shared x
2620 %! x = [1, -3, 4, 5, -7];
2621 %!assert(norm(x,1), 20);
2622 %!assert(norm(x,2), 10);
2623 %!assert(norm(x,3), 8.24257059961711, -4*eps);
2624 %!assert(norm(x,Inf), 7);
2625 %!assert(norm(x,-Inf), 1);
2626 %!assert(norm(x,"inf"), 7);
2627 %!assert(norm(x,"fro"), 10);
2628 %!assert(norm(x), 10);
2629 %!assert(norm([1e200, 1]), 1e200);
2630 %!assert(norm([3+4i, 3-4i, sqrt(31)]), 9, -4*eps);
2631 %!shared m
2632 %! m = magic (4);
2633 %!assert(norm(m,1), 34);
2634 %!assert(norm(m,2), 34);
2635 %!assert(norm(m,Inf), 34);
2636 %!assert(norm(m,"inf"), 34);
2637 */
2638
2617 // Compute various norms of the vector X. 2639 // Compute various norms of the vector X.
2618 2640
2619 DEFUN (__vnorm__, args, , 2641 DEFUN (norm, args, ,
2620 "-*- texinfo -*-\n\ 2642 "-*- texinfo -*-\n\
2621 @deftypefn {Built-in Function} {} __vnorm__ (@var{x}, @var{p})\n\ 2643 @deftypefn {Function File} {} norm (@var{a}, @var{p})\n\
2622 Undocumented internal function.\n\ 2644 Compute the p-norm of the matrix @var{a}. If the second argument is\n\
2623 @end deftypefn") 2645 missing, @code{p = 2} is assumed.\n\
2624 { 2646 \n\
2625 octave_value retval; 2647 If @var{a} is a matrix:\n\
2648 \n\
2649 @table @asis\n\
2650 @item @var{p} = @code{1}\n\
2651 1-norm, the largest column sum of the absolute values of @var{a}.\n\
2652 \n\
2653 @item @var{p} = @code{2}\n\
2654 Largest singular value of @var{a}.\n\
2655 \n\
2656 @item @var{p} = @code{Inf}\n\
2657 @cindex infinity norm\n\
2658 Infinity norm, the largest row sum of the absolute values of @var{a}.\n\
2659 \n\
2660 @item @var{p} = @code{\"fro\"}\n\
2661 @cindex Frobenius norm\n\
2662 Frobenius norm of @var{a}, @code{sqrt (sum (diag (@var{a}' * @var{a})))}.\n\
2663 @end table\n\
2664 \n\
2665 If @var{a} is a vector or a scalar:\n\
2666 \n\
2667 @table @asis\n\
2668 @item @var{p} = @code{Inf}\n\
2669 @code{max (abs (@var{a}))}.\n\
2670 \n\
2671 @item @var{p} = @code{-Inf}\n\
2672 @code{min (abs (@var{a}))}.\n\
2673 \n\
2674 @item other\n\
2675 p-norm of @var{a}, @code{(sum (abs (@var{a}) .^ @var{p})) ^ (1/@var{p})}.\n\
2676 @end table\n\
2677 @seealso{cond, svd}\n\
2678 @end deftypefn")
2679 {
2680 // Currently only handles vector norms for full double/complex
2681 // vectors internally. Other cases are handled by __norm__.m.
2682
2683 octave_value_list retval;
2626 2684
2627 int nargin = args.length (); 2685 int nargin = args.length ();
2628 2686
2629 if (nargin == 1 || nargin == 2) 2687 if (nargin == 1 || nargin == 2)
2630 { 2688 {
2631 double p_val; 2689 octave_value x_arg = args(0);
2632 2690
2633 octave_value p_arg; 2691 if (x_arg.is_empty ())
2634 2692 retval(0) = 0.0;
2635 if (nargin == 1) 2693 else if (x_arg.ndims () == 2)
2636 p_arg = 2; 2694 {
2695 if ((x_arg.rows () == 1 || x_arg.columns () == 1)
2696 && ! (x_arg.is_sparse_type () || x_arg.is_integer_type ()))
2697 {
2698 double p_val;
2699
2700 octave_value p_arg;
2701
2702 if (nargin == 1)
2703 p_arg = 2;
2704 else
2705 p_arg = args(1);
2706
2707 if (p_arg.is_string ())
2708 {
2709 std::string p = args(1).string_value ();
2710
2711 if (p == "inf")
2712 p_val = octave_Inf;
2713 else if (p == "fro")
2714 p_val = -1;
2715 else
2716 error ("norm: unrecognized norm `%s'", p.c_str ());
2717 }
2718 else
2719 {
2720 p_val = p_arg.double_value ();
2721
2722 if (error_state)
2723 error ("norm: unrecognized norm value");
2724 }
2725
2726 if (! error_state)
2727 {
2728 if (x_arg.is_real_type ())
2729 {
2730 MArray<double> x (x_arg.array_value ());
2731
2732 if (! error_state)
2733 retval(0) = x.norm (p_val);
2734 else
2735 error ("norm: expecting real vector");
2736 }
2737 else
2738 {
2739 MArray<Complex> x (x_arg.complex_array_value ());
2740
2741 if (! error_state)
2742 retval(0) = x.norm (p_val);
2743 else
2744 error ("norm: expecting complex vector");
2745 }
2746 }
2747 }
2748 else
2749 retval = feval ("__norm__", args);
2750 }
2637 else 2751 else
2638 p_arg = args(1); 2752 error ("norm: only valid for 2-D objects");
2639
2640 if (p_arg.is_string ())
2641 {
2642 std::string p = args(1).string_value ();
2643
2644 if (p == "inf")
2645 p_val = octave_Inf;
2646 else if (p == "fro")
2647 p_val = -1;
2648 else
2649 {
2650 error ("norm: unrecognized norm `%s'", p.c_str ());
2651 return retval;
2652 }
2653 }
2654 else
2655 {
2656 p_val = args(1).double_value ();
2657
2658 if (error_state)
2659 {
2660 error ("norm: unrecognized norm value");
2661 return retval;
2662 }
2663 }
2664
2665 octave_value x_arg = args(0);
2666
2667 if (x_arg.is_real_type ())
2668 {
2669 ColumnVector x (x_arg.vector_value ());
2670
2671 if (! error_state)
2672 retval = x.norm (p_val);
2673 else
2674 error ("norm: expecting real vector");
2675 }
2676 else
2677 {
2678 ComplexColumnVector x (x_arg.complex_vector_value ());
2679
2680 if (! error_state)
2681 retval = x.norm (p_val);
2682 else
2683 error ("norm: expecting complex vector");
2684 }
2685 } 2753 }
2686 else 2754 else
2687 print_usage (); 2755 print_usage ();
2688 2756
2689 return retval; 2757 return retval;