1 /* 2 This code was extracted from liblinear-2.2.1 in Feb 2019 and 3 modified for the use with Octave and Matlab 4 5 6 Copyright (c) 2007-2019 The LIBLINEAR Project. 7 All rights reserved. 8 9 Redistribution and use in source and binary forms, with or without 10 modification, are permitted provided that the following conditions 11 are met: 12 13 1. Redistributions of source code must retain the above copyright 14 notice, this list of conditions and the following disclaimer. 15 16 2. Redistributions in binary form must reproduce the above copyright 17 notice, this list of conditions and the following disclaimer in the 18 documentation and/or other materials provided with the distribution. 19 20 3. Neither name of copyright holders nor the names of its contributors 21 may be used to endorse or promote products derived from this software 22 without specific prior written permission. 23 24 25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 26 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 27 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 28 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 29 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 30 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 31 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 32 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 33 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 34 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 35 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 37 */ 38 39 #ifndef _TRON_H 40 #define _TRON_H 41 42 class function 43 { 44 public: 45 virtual double fun(double *w) = 0 ; 46 virtual void grad(double *w, double *g) = 0 ; 47 virtual void Hv(double *s, double *Hs) = 0 ; 48 49 virtual int get_nr_variable(void) = 0 ; 50 virtual void get_diag_preconditioner(double *M) = 0 ; ~function(void)51 virtual ~function(void){} 52 }; 53 54 class TRON 55 { 56 public: 57 TRON(const function *fun_obj, double eps = 0.1, double eps_cg = 0.1, int max_iter = 1000); 58 ~TRON(); 59 60 void tron(double *w); 61 void set_print_string(void (*i_print) (const char *buf)); 62 63 private: 64 int trpcg(double delta, double *g, double *M, double *s, double *r, bool *reach_boundary); 65 double norm_inf(int n, double *x); 66 67 double eps; 68 double eps_cg; 69 int max_iter; 70 function *fun_obj; 71 void info(const char *fmt,...); 72 void (*tron_print_string)(const char *buf); 73 }; 74 #endif 75