File: __disna_optim__.cc

package info (click to toggle)
octave-optim 1.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,368 kB
  • sloc: cpp: 1,437; makefile: 185; perl: 169; xml: 29; sh: 3
file content (179 lines) | stat: -rw-r--r-- 5,046 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (C) 2011-2016 Olaf Till <olaf.till@uni-jena.de>
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation; either version 3 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, see <http://www.gnu.org/licenses/>.

// This function has also been submitted to Octave (bug #33503).

#include <octave/oct.h>
#include <f77-fcn.h>

#include "error-helpers.h"

#if defined (OCTAVE_HAVE_F77_INT_TYPE)
#  define TO_F77_INT(x) octave::to_f77_int (x)
#else
typedef octave_idx_type F77_INT;
#  define TO_F77_INT(x) (x)
#endif

extern "C"
{
  F77_RET_T
  F77_FUNC (ddisna, DDISNA) (F77_CONST_CHAR_ARG_DECL,
                             const F77_INT&,
                             const F77_INT&,
                             const double*,
                             double*,
                             F77_INT&);

  F77_RET_T
  F77_FUNC (sdisna, SDISNA) (F77_CONST_CHAR_ARG_DECL,
                             const F77_INT&,
                             const F77_INT&,
                             const float*,
                             float*,
                             F77_INT&);
}

DEFUN_DLD (__disna_optim__, args, ,
  "-*- texinfo -*-\n\
@deftypefn {Loadable Function} {@var{rcond} =} __disna__ (@var{job}, @var{d})\n\
@deftypefnx {Loadable Function} {@var{rcond} =} __disna__ (@var{job}, @var{d}, @var{m}, @var{n})\n\
Undocumented internal function.\n\
@end deftypefn")
{
  /*
    Interface to DDISNA and SDISNA of LAPACK.

    If job is 'E', no third or fourth argument are given. If job is 'L'
    or 'R', M and N are given.
  */

  std::string fname ("__disna__");

  octave_value retval;

  if (args.length () != 2 && args.length () != 4)
    {
      print_usage ();
      return retval;
    }

  std::string job_str;
  CHECK_ERROR (job_str = args(0).string_value (), retval,
               "%s: job label no string", fname.c_str ());

  char job;

  if (job_str.length () != 1)
    {
      error ("%s: invalid job label", fname.c_str ());
      return retval;
    }
  else
    job = job_str[0];

  F77_INT m, n, l;
  bool single;
  octave_value d;

  if (args(1).is_single_type ())
    {
      single = true;
      CHECK_ERROR (d = args(1).float_column_vector_value (), retval,
                   "s: invalid arguments", fname.c_str ());
    }
  else
    {
      single = false;
      CHECK_ERROR (d = args(1).column_vector_value (), retval,
                   "s: invalid arguments", fname.c_str ());
    }

  l = d.length ();
  switch (job)
    {
    case 'E' :
      if (args.length () != 2)
        {
          error ("%s: with job label 'E' only two arguments are allowed",
                 fname.c_str ());
          return retval;
        }
      else
        m = l;
      break;
    case 'L' :
    case 'R' :
      if (args.length () != 4)
        {
          error ("%s: with job labels 'L' or 'R', four arguments must be given",
                 fname.c_str ());
          return retval;
        }
      else
        {
          CHECK_ERROR (m = TO_F77_INT (args(2).idx_type_value ()), retval,
                       "%s: could not convert 3rd argumet to index",
                       fname.c_str ());
          CHECK_ERROR (n = TO_F77_INT (args(3).idx_type_value ()), retval,
                       "%s: could not convert 4th argumet to index",
                       fname.c_str ());
          F77_INT md = m < n ? m : n;
          if (l != md)
            {
              error ("%s: given dimensions don't match length of second argument",
                     fname.c_str ());
              return retval;
            }
        }
      break;
    default :
      error ("%s: job label not correct", fname.c_str ());
      return retval;
    }

  F77_INT info;

  if (single)
    {
      FloatColumnVector srcond (l);

      F77_XFCN (sdisna, SDISNA, (F77_CONST_CHAR_ARG2 (&job, 1),
                                 m, n,
                                 d.float_column_vector_value ().fortran_vec (),
                                 srcond.fortran_vec (),
                                 info));

      retval = srcond;
    }
  else
    {
      ColumnVector drcond (l);

      F77_XFCN (ddisna, DDISNA, (F77_CONST_CHAR_ARG2 (&job, 1),
                                 m, n,
                                 d.column_vector_value ().fortran_vec (),
                                 drcond.fortran_vec (),
                                 info));

      retval = drcond;
    }

  if (info < 0)
    error ("%s: LAPACK routine says %i-th argument had an illegal value",
           fname.c_str (), -info);

  return retval;
}