xref: /petsc/src/ml/regressor/interface/regressor.c (revision 34b254c57d2aa195261fbc0db2d1455fb6d091da) !
1*34b254c5SRichard Tran Mills #include <petsc/private/regressorimpl.h>
2*34b254c5SRichard Tran Mills 
3*34b254c5SRichard Tran Mills PetscBool         PetscRegressorRegisterAllCalled = PETSC_FALSE;
4*34b254c5SRichard Tran Mills PetscFunctionList PetscRegressorList              = NULL;
5*34b254c5SRichard Tran Mills 
6*34b254c5SRichard Tran Mills PetscClassId PETSCREGRESSOR_CLASSID;
7*34b254c5SRichard Tran Mills 
8*34b254c5SRichard Tran Mills /* Logging support */
9*34b254c5SRichard Tran Mills PetscLogEvent PetscRegressor_SetUp, PetscRegressor_Fit, PetscRegressor_Predict;
10*34b254c5SRichard Tran Mills 
11*34b254c5SRichard Tran Mills /*@C
12*34b254c5SRichard Tran Mills   PetscRegressorRegister - Adds a method to the `PetscRegressor` package.
13*34b254c5SRichard Tran Mills 
14*34b254c5SRichard Tran Mills   Not collective
15*34b254c5SRichard Tran Mills 
16*34b254c5SRichard Tran Mills   Input Parameters:
17*34b254c5SRichard Tran Mills + sname    - name of a new user-defined regressor
18*34b254c5SRichard Tran Mills - function - routine to create method context
19*34b254c5SRichard Tran Mills 
20*34b254c5SRichard Tran Mills   Notes:
21*34b254c5SRichard Tran Mills   `PetscRegressorRegister()` may be called multiple times to add several user-defined regressors.
22*34b254c5SRichard Tran Mills 
23*34b254c5SRichard Tran Mills   Example Usage:
24*34b254c5SRichard Tran Mills .vb
25*34b254c5SRichard Tran Mills    PetscRegressorRegister("my_regressor",MyRegressorCreate);
26*34b254c5SRichard Tran Mills .ve
27*34b254c5SRichard Tran Mills 
28*34b254c5SRichard Tran Mills   Then, your regressor can be chosen with the procedural interface via
29*34b254c5SRichard Tran Mills .vb
30*34b254c5SRichard Tran Mills      PetscRegressorSetType(regressor,"my_regressor")
31*34b254c5SRichard Tran Mills .ve
32*34b254c5SRichard Tran Mills   or at runtime via the option
33*34b254c5SRichard Tran Mills .vb
34*34b254c5SRichard Tran Mills     -regressor_type my_regressor
35*34b254c5SRichard Tran Mills .ve
36*34b254c5SRichard Tran Mills 
37*34b254c5SRichard Tran Mills   Level: advanced
38*34b254c5SRichard Tran Mills 
39*34b254c5SRichard Tran Mills .seealso: `PetscRegressorRegisterAll()`
40*34b254c5SRichard Tran Mills @*/
41*34b254c5SRichard Tran Mills PetscErrorCode PetscRegressorRegister(const char sname[], PetscErrorCode (*function)(PetscRegressor))
42*34b254c5SRichard Tran Mills {
43*34b254c5SRichard Tran Mills   PetscFunctionBegin;
44*34b254c5SRichard Tran Mills   PetscCall(PetscRegressorInitializePackage());
45*34b254c5SRichard Tran Mills   PetscCall(PetscFunctionListAdd(&PetscRegressorList, sname, function));
46*34b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
47*34b254c5SRichard Tran Mills }
48*34b254c5SRichard Tran Mills 
49*34b254c5SRichard Tran Mills /*@
50*34b254c5SRichard Tran Mills   PetscRegressorCreate - Creates a `PetscRegressor` object.
51*34b254c5SRichard Tran Mills 
52*34b254c5SRichard Tran Mills   Collective
53*34b254c5SRichard Tran Mills 
54*34b254c5SRichard Tran Mills   Input Parameter:
55*34b254c5SRichard Tran Mills . comm - the MPI communicator that will share the `PetscRegressor` object
56*34b254c5SRichard Tran Mills 
57*34b254c5SRichard Tran Mills   Output Parameter:
58*34b254c5SRichard Tran Mills . newregressor - the new `PetscRegressor` object
59*34b254c5SRichard Tran Mills 
60*34b254c5SRichard Tran Mills   Level: beginner
61*34b254c5SRichard Tran Mills 
62*34b254c5SRichard Tran Mills .seealso: `PetscRegressorFit()`, `PetscRegressorPredict()`, `PetscRegressor`
63*34b254c5SRichard Tran Mills @*/
64*34b254c5SRichard Tran Mills PetscErrorCode PetscRegressorCreate(MPI_Comm comm, PetscRegressor *newregressor)
65*34b254c5SRichard Tran Mills {
66*34b254c5SRichard Tran Mills   PetscRegressor regressor;
67*34b254c5SRichard Tran Mills 
68*34b254c5SRichard Tran Mills   PetscFunctionBegin;
69*34b254c5SRichard Tran Mills   PetscAssertPointer(newregressor, 2);
70*34b254c5SRichard Tran Mills   *newregressor = NULL;
71*34b254c5SRichard Tran Mills   PetscCall(PetscRegressorInitializePackage());
72*34b254c5SRichard Tran Mills 
73*34b254c5SRichard Tran Mills   PetscCall(PetscHeaderCreate(regressor, PETSCREGRESSOR_CLASSID, "PetscRegressor", "Regressor", "PetscRegressor", comm, PetscRegressorDestroy, PetscRegressorView));
74*34b254c5SRichard Tran Mills 
75*34b254c5SRichard Tran Mills   regressor->setupcalled = PETSC_FALSE;
76*34b254c5SRichard Tran Mills   regressor->fitcalled   = PETSC_FALSE;
77*34b254c5SRichard Tran Mills   regressor->data        = NULL;
78*34b254c5SRichard Tran Mills   regressor->training    = NULL;
79*34b254c5SRichard Tran Mills   regressor->target      = NULL;
80*34b254c5SRichard Tran Mills   PetscObjectParameterSetDefault(regressor, regularizer_weight, 1.0); // Default to regularizer weight of 1.0, usually the default in SciKit-learn
81*34b254c5SRichard Tran Mills 
82*34b254c5SRichard Tran Mills   *newregressor = regressor;
83*34b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
84*34b254c5SRichard Tran Mills }
85*34b254c5SRichard Tran Mills 
86*34b254c5SRichard Tran Mills /*@
87*34b254c5SRichard Tran Mills   PetscRegressorView - Prints information about the `PetscRegressor` object
88*34b254c5SRichard Tran Mills 
89*34b254c5SRichard Tran Mills   Collective
90*34b254c5SRichard Tran Mills 
91*34b254c5SRichard Tran Mills   Input Parameters:
92*34b254c5SRichard Tran Mills + regressor - the `PetscRegressor` context
93*34b254c5SRichard Tran Mills - viewer    - a `PetscViewer` context
94*34b254c5SRichard Tran Mills 
95*34b254c5SRichard Tran Mills   Options Database Key:
96*34b254c5SRichard Tran Mills . -regressor_view - Calls `PetscRegressorView()` at the end of `PetscRegressorFit()`
97*34b254c5SRichard Tran Mills 
98*34b254c5SRichard Tran Mills   Level: beginner
99*34b254c5SRichard Tran Mills 
100*34b254c5SRichard Tran Mills   Notes:
101*34b254c5SRichard Tran Mills   The available visualization contexts include
102*34b254c5SRichard Tran Mills +     `PETSC_VIEWER_STDOUT_SELF` - standard output (default)
103*34b254c5SRichard Tran Mills -     `PETSC_VIEWER_STDOUT_WORLD` - synchronized standard
104*34b254c5SRichard Tran Mills   output where only the first processor opens
105*34b254c5SRichard Tran Mills   the file.  All other processors send their
106*34b254c5SRichard Tran Mills   data to the first processor to print.
107*34b254c5SRichard Tran Mills 
108*34b254c5SRichard Tran Mills .seealso: [](ch_regressor), `PetscRegressor`, `PetscViewerASCIIOpen()`
109*34b254c5SRichard Tran Mills @*/
110*34b254c5SRichard Tran Mills PetscErrorCode PetscRegressorView(PetscRegressor regressor, PetscViewer viewer)
111*34b254c5SRichard Tran Mills {
112*34b254c5SRichard Tran Mills   PetscBool          isascii, isstring;
113*34b254c5SRichard Tran Mills   PetscRegressorType type;
114*34b254c5SRichard Tran Mills 
115*34b254c5SRichard Tran Mills   PetscFunctionBegin;
116*34b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
117*34b254c5SRichard Tran Mills   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(((PetscObject)regressor)->comm, &viewer));
118*34b254c5SRichard Tran Mills   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
119*34b254c5SRichard Tran Mills   PetscCheckSameComm(regressor, 1, viewer, 2);
120*34b254c5SRichard Tran Mills 
121*34b254c5SRichard Tran Mills   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
122*34b254c5SRichard Tran Mills   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring));
123*34b254c5SRichard Tran Mills   if (isascii) {
124*34b254c5SRichard Tran Mills     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)regressor, viewer));
125*34b254c5SRichard Tran Mills 
126*34b254c5SRichard Tran Mills     PetscCall(PetscViewerASCIIPushTab(viewer));
127*34b254c5SRichard Tran Mills     PetscTryTypeMethod(regressor, view, viewer);
128*34b254c5SRichard Tran Mills     if (regressor->tao) PetscCall(TaoView(regressor->tao, viewer));
129*34b254c5SRichard Tran Mills     PetscCall(PetscViewerASCIIPopTab(viewer));
130*34b254c5SRichard Tran Mills   } else if (isstring) {
131*34b254c5SRichard Tran Mills     PetscCall(PetscRegressorGetType(regressor, &type));
132*34b254c5SRichard Tran Mills     PetscCall(PetscViewerStringSPrintf(viewer, " PetscRegressorType: %-7.7s", type));
133*34b254c5SRichard Tran Mills   }
134*34b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
135*34b254c5SRichard Tran Mills }
136*34b254c5SRichard Tran Mills 
137*34b254c5SRichard Tran Mills /*@
138*34b254c5SRichard Tran Mills   PetscRegressorViewFromOptions - View a `PetscRegressor` object based on values in the options database
139*34b254c5SRichard Tran Mills 
140*34b254c5SRichard Tran Mills   Collective
141*34b254c5SRichard Tran Mills 
142*34b254c5SRichard Tran Mills   Input Parameters:
143*34b254c5SRichard Tran Mills + A    - the  `PetscRegressor` context
144*34b254c5SRichard Tran Mills . obj  - Optional object that provides the prefix for the options database
145*34b254c5SRichard Tran Mills - name - command line option
146*34b254c5SRichard Tran Mills 
147*34b254c5SRichard Tran Mills   Level: intermediate
148*34b254c5SRichard Tran Mills 
149*34b254c5SRichard Tran Mills .seealso: [](ch_regressor), `PetscRegressor`, `PetscRegressorView`, `PetscObjectViewFromOptions()`, `PetscRegressorCreate()`
150*34b254c5SRichard Tran Mills @*/
151*34b254c5SRichard Tran Mills PetscErrorCode PetscRegressorViewFromOptions(PetscRegressor A, PetscObject obj, const char name[])
152*34b254c5SRichard Tran Mills {
153*34b254c5SRichard Tran Mills   PetscFunctionBegin;
154*34b254c5SRichard Tran Mills   PetscValidHeaderSpecific(A, PETSCREGRESSOR_CLASSID, 1);
155*34b254c5SRichard Tran Mills   PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
156*34b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
157*34b254c5SRichard Tran Mills }
158*34b254c5SRichard Tran Mills 
159*34b254c5SRichard Tran Mills /*@
160*34b254c5SRichard Tran Mills   PetscRegressorSetFromOptions - Sets `PetscRegressor` options from the options database.
161*34b254c5SRichard Tran Mills 
162*34b254c5SRichard Tran Mills   Collective
163*34b254c5SRichard Tran Mills 
164*34b254c5SRichard Tran Mills   Input Parameter:
165*34b254c5SRichard Tran Mills . regressor - the `PetscRegressor` context
166*34b254c5SRichard Tran Mills 
167*34b254c5SRichard Tran Mills   Options Database Keys:
168*34b254c5SRichard Tran Mills . -regressor_type <type> - the particular type of regressor to be used; see `PetscRegressorType` for complete list
169*34b254c5SRichard Tran Mills 
170*34b254c5SRichard Tran Mills   Level: beginner
171*34b254c5SRichard Tran Mills 
172*34b254c5SRichard Tran Mills   Note:
173*34b254c5SRichard Tran Mills   This routine must be called before `PetscRegressorSetUp()` (or `PetscRegressorFit()`, which calls
174*34b254c5SRichard Tran Mills   the former) if the user is to be allowed to set the regressor type.
175*34b254c5SRichard Tran Mills 
176*34b254c5SRichard Tran Mills .seealso: `PetscRegressor`, `PetscRegressorCreate()`
177*34b254c5SRichard Tran Mills @*/
178*34b254c5SRichard Tran Mills PetscErrorCode PetscRegressorSetFromOptions(PetscRegressor regressor)
179*34b254c5SRichard Tran Mills {
180*34b254c5SRichard Tran Mills   PetscBool          flg;
181*34b254c5SRichard Tran Mills   PetscRegressorType default_type = PETSCREGRESSORLINEAR;
182*34b254c5SRichard Tran Mills   char               type[256];
183*34b254c5SRichard Tran Mills 
184*34b254c5SRichard Tran Mills   PetscFunctionBegin;
185*34b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
186*34b254c5SRichard Tran Mills   if (((PetscObject)regressor)->type_name) default_type = ((PetscObject)regressor)->type_name;
187*34b254c5SRichard Tran Mills   PetscObjectOptionsBegin((PetscObject)regressor);
188*34b254c5SRichard Tran Mills   /* Check for type from options */
189*34b254c5SRichard Tran Mills   PetscCall(PetscOptionsFList("-regressor_type", "PetscRegressor type", "PetscRegressorSetType", PetscRegressorList, default_type, type, 256, &flg));
190*34b254c5SRichard Tran Mills   if (flg) {
191*34b254c5SRichard Tran Mills     PetscCall(PetscRegressorSetType(regressor, type));
192*34b254c5SRichard Tran Mills   } else if (!((PetscObject)regressor)->type_name) {
193*34b254c5SRichard Tran Mills     PetscCall(PetscRegressorSetType(regressor, default_type));
194*34b254c5SRichard Tran Mills   }
195*34b254c5SRichard Tran Mills   PetscCall(PetscOptionsReal("-regressor_regularizer_weight", "Weight for the regularizer", "PetscRegressorSetRegularizerWeight", regressor->regularizer_weight, &regressor->regularizer_weight, &flg));
196*34b254c5SRichard Tran Mills   if (flg) PetscCall(PetscRegressorSetRegularizerWeight(regressor, regressor->regularizer_weight));
197*34b254c5SRichard Tran Mills   // The above is a little superfluous, because we have already set regressor->regularizer_weight above, but we also need to set the flag indicating that the user has set the weight!
198*34b254c5SRichard Tran Mills   PetscTryTypeMethod(regressor, setfromoptions, PetscOptionsObject);
199*34b254c5SRichard Tran Mills   PetscOptionsEnd();
200*34b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
201*34b254c5SRichard Tran Mills }
202*34b254c5SRichard Tran Mills 
203*34b254c5SRichard Tran Mills /*@
204*34b254c5SRichard Tran Mills   PetscRegressorSetUp - Sets up the internal data structures for the later use of a regressor.
205*34b254c5SRichard Tran Mills 
206*34b254c5SRichard Tran Mills   Collective
207*34b254c5SRichard Tran Mills 
208*34b254c5SRichard Tran Mills   Input Parameter:
209*34b254c5SRichard Tran Mills . regressor - the `PetscRegressor` context
210*34b254c5SRichard Tran Mills 
211*34b254c5SRichard Tran Mills   Notes:
212*34b254c5SRichard Tran Mills   For basic use of the `PetscRegressor` solvers the user need not to explicitly call
213*34b254c5SRichard Tran Mills   `PetscRegressorSetUp()`, since these actions will automatically occur during
214*34b254c5SRichard Tran Mills   the call to `PetscRegressorFit()`.  However, if one wishes to control this
215*34b254c5SRichard Tran Mills   phase separately, `PetscRegressorSetUp()` should be called after `PetscRegressorCreate()`,
216*34b254c5SRichard Tran Mills   `PetscRegressorSetUp()`, and optional routines of the form `PetscRegressorSetXXX()`,
217*34b254c5SRichard Tran Mills   but before `PetscRegressorFit()`.
218*34b254c5SRichard Tran Mills 
219*34b254c5SRichard Tran Mills   Level: advanced
220*34b254c5SRichard Tran Mills 
221*34b254c5SRichard Tran Mills .seealso: `PetscRegressorCreate()`, `PetscRegressorFit()`, `PetscRegressorDestroy()`
222*34b254c5SRichard Tran Mills @*/
223*34b254c5SRichard Tran Mills PetscErrorCode PetscRegressorSetUp(PetscRegressor regressor)
224*34b254c5SRichard Tran Mills {
225*34b254c5SRichard Tran Mills   PetscFunctionBegin;
226*34b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
227*34b254c5SRichard Tran Mills   if (regressor->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
228*34b254c5SRichard Tran Mills   PetscCall(PetscLogEventBegin(PetscRegressor_SetUp, regressor, 0, 0, 0));
229*34b254c5SRichard Tran Mills   //TODO is there some mat vec etc that must be set, like TaoSolution?
230*34b254c5SRichard Tran Mills   PetscTryTypeMethod(regressor, setup);
231*34b254c5SRichard Tran Mills   regressor->setupcalled = PETSC_TRUE;
232*34b254c5SRichard Tran Mills   PetscCall(PetscLogEventEnd(PetscRegressor_SetUp, regressor, 0, 0, 0));
233*34b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
234*34b254c5SRichard Tran Mills }
235*34b254c5SRichard Tran Mills 
236*34b254c5SRichard Tran Mills /* NOTE: I've decided to make this take X and y, like the Scikit-learn Fit routines do.
237*34b254c5SRichard Tran Mills  * Am I overlooking some reason that X should be set in a separate function call, a la KSPSetOperators()?. */
238*34b254c5SRichard Tran Mills /*@
239*34b254c5SRichard Tran Mills   PetscRegressorFit - Fit, or train, a regressor from a training dataset
240*34b254c5SRichard Tran Mills 
241*34b254c5SRichard Tran Mills   Collective
242*34b254c5SRichard Tran Mills 
243*34b254c5SRichard Tran Mills   Input Parameters:
244*34b254c5SRichard Tran Mills + regressor - the `PetscRegressor` context
245*34b254c5SRichard Tran Mills . X         - matrix of training data (of dimension [number of samples] x [number of features])
246*34b254c5SRichard Tran Mills - y         - vector of target values from the training dataset
247*34b254c5SRichard Tran Mills 
248*34b254c5SRichard Tran Mills   Level: beginner
249*34b254c5SRichard Tran Mills 
250*34b254c5SRichard Tran Mills .seealso: `PetscRegressorCreate()`, `PetscRegressorSetUp()`, `PetscRegressorDestroy()`, `PetscRegressorPredict()`
251*34b254c5SRichard Tran Mills @*/
252*34b254c5SRichard Tran Mills PetscErrorCode PetscRegressorFit(PetscRegressor regressor, Mat X, Vec y)
253*34b254c5SRichard Tran Mills {
254*34b254c5SRichard Tran Mills   PetscFunctionBegin;
255*34b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
256*34b254c5SRichard Tran Mills   if (X) PetscValidHeaderSpecific(X, MAT_CLASSID, 2);
257*34b254c5SRichard Tran Mills   if (y) PetscValidHeaderSpecific(y, VEC_CLASSID, 3);
258*34b254c5SRichard Tran Mills 
259*34b254c5SRichard Tran Mills   if (X) {
260*34b254c5SRichard Tran Mills     PetscCall(PetscObjectReference((PetscObject)X));
261*34b254c5SRichard Tran Mills     PetscCall(MatDestroy(&regressor->training));
262*34b254c5SRichard Tran Mills     regressor->training = X;
263*34b254c5SRichard Tran Mills   }
264*34b254c5SRichard Tran Mills   if (y) {
265*34b254c5SRichard Tran Mills     PetscCall(PetscObjectReference((PetscObject)y));
266*34b254c5SRichard Tran Mills     PetscCall(VecDestroy(&regressor->target));
267*34b254c5SRichard Tran Mills     regressor->target = y;
268*34b254c5SRichard Tran Mills   }
269*34b254c5SRichard Tran Mills   PetscCall(PetscRegressorSetUp(regressor));
270*34b254c5SRichard Tran Mills 
271*34b254c5SRichard Tran Mills   PetscCall(PetscLogEventBegin(PetscRegressor_Fit, regressor, X, y, 0));
272*34b254c5SRichard Tran Mills   PetscUseTypeMethod(regressor, fit);
273*34b254c5SRichard Tran Mills   PetscCall(PetscLogEventEnd(PetscRegressor_Fit, regressor, X, y, 0));
274*34b254c5SRichard Tran Mills   //TODO print convergence data
275*34b254c5SRichard Tran Mills   PetscCall(PetscRegressorViewFromOptions(regressor, NULL, "-regressor_view"));
276*34b254c5SRichard Tran Mills   regressor->fitcalled = PETSC_TRUE;
277*34b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
278*34b254c5SRichard Tran Mills }
279*34b254c5SRichard Tran Mills 
280*34b254c5SRichard Tran Mills /*@
281*34b254c5SRichard Tran Mills   PetscRegressorPredict - Compute predictions (that is, perform inference) using a fitted regression model.
282*34b254c5SRichard Tran Mills 
283*34b254c5SRichard Tran Mills   Collective
284*34b254c5SRichard Tran Mills 
285*34b254c5SRichard Tran Mills   Input Parameters:
286*34b254c5SRichard Tran Mills + regressor - the `PetscRegressor` context (for which `PetscRegressorFit()` must have been called)
287*34b254c5SRichard Tran Mills - X         - data matrix of unlabeled observations
288*34b254c5SRichard Tran Mills 
289*34b254c5SRichard Tran Mills   Output Parameter:
290*34b254c5SRichard Tran Mills . y - vector of predicted labels
291*34b254c5SRichard Tran Mills 
292*34b254c5SRichard Tran Mills   Level: beginner
293*34b254c5SRichard Tran Mills 
294*34b254c5SRichard Tran Mills .seealso: `PetscRegressorFit()`, `PetscRegressorDestroy()`
295*34b254c5SRichard Tran Mills @*/
296*34b254c5SRichard Tran Mills PetscErrorCode PetscRegressorPredict(PetscRegressor regressor, Mat X, Vec y)
297*34b254c5SRichard Tran Mills {
298*34b254c5SRichard Tran Mills   PetscFunctionBegin;
299*34b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
300*34b254c5SRichard Tran Mills   if (X) PetscValidHeaderSpecific(X, MAT_CLASSID, 2);
301*34b254c5SRichard Tran Mills   if (y) PetscValidHeaderSpecific(y, VEC_CLASSID, 3);
302*34b254c5SRichard Tran Mills   PetscCheck(regressor->fitcalled == PETSC_TRUE, ((PetscObject)regressor)->comm, PETSC_ERR_ARG_WRONGSTATE, "PetscRegressorFit() must be called before PetscRegressorPredict()");
303*34b254c5SRichard Tran Mills   PetscCall(PetscLogEventBegin(PetscRegressor_Predict, regressor, X, y, 0));
304*34b254c5SRichard Tran Mills   PetscTryTypeMethod(regressor, predict, X, y);
305*34b254c5SRichard Tran Mills   PetscCall(PetscLogEventEnd(PetscRegressor_Predict, regressor, X, y, 0));
306*34b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
307*34b254c5SRichard Tran Mills }
308*34b254c5SRichard Tran Mills 
309*34b254c5SRichard Tran Mills /*@
310*34b254c5SRichard Tran Mills   PetscRegressorReset - Resets a `PetscRegressor` context by removing any allocated `Vec` and `Mat`. Any options set in the object remain.
311*34b254c5SRichard Tran Mills 
312*34b254c5SRichard Tran Mills   Collective
313*34b254c5SRichard Tran Mills 
314*34b254c5SRichard Tran Mills   Input Parameter:
315*34b254c5SRichard Tran Mills . regressor - context obtained from `PetscRegressorCreate()`
316*34b254c5SRichard Tran Mills 
317*34b254c5SRichard Tran Mills   Level: intermediate
318*34b254c5SRichard Tran Mills 
319*34b254c5SRichard Tran Mills .seealso: `PetscRegressorCreate()`, `PetscRegressorSetUp()`, `PetscRegressorFit()`, `PetscRegressorPredict()`, `PetscRegressorDestroy()`
320*34b254c5SRichard Tran Mills @*/
321*34b254c5SRichard Tran Mills PetscErrorCode PetscRegressorReset(PetscRegressor regressor)
322*34b254c5SRichard Tran Mills {
323*34b254c5SRichard Tran Mills   PetscFunctionBegin;
324*34b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
325*34b254c5SRichard Tran Mills   if (regressor->ops->reset) PetscTryTypeMethod(regressor, reset);
326*34b254c5SRichard Tran Mills   PetscCall(MatDestroy(&regressor->training));
327*34b254c5SRichard Tran Mills   PetscCall(VecDestroy(&regressor->target));
328*34b254c5SRichard Tran Mills   PetscCall(TaoDestroy(&regressor->tao));
329*34b254c5SRichard Tran Mills   regressor->setupcalled = PETSC_FALSE;
330*34b254c5SRichard Tran Mills   regressor->fitcalled   = PETSC_FALSE;
331*34b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
332*34b254c5SRichard Tran Mills }
333*34b254c5SRichard Tran Mills 
334*34b254c5SRichard Tran Mills /*@C
335*34b254c5SRichard Tran Mills   PetscRegressorDestroy - Destroys the regressor context that was created with `PetscRegressorCreate()`.
336*34b254c5SRichard Tran Mills 
337*34b254c5SRichard Tran Mills   Collective
338*34b254c5SRichard Tran Mills 
339*34b254c5SRichard Tran Mills   Input Parameter:
340*34b254c5SRichard Tran Mills . regressor - the `PetscRegressor` context
341*34b254c5SRichard Tran Mills 
342*34b254c5SRichard Tran Mills   Level: beginner
343*34b254c5SRichard Tran Mills 
344*34b254c5SRichard Tran Mills .seealso: `PetscRegressorCreate()`, `PetscRegressorSetUp()`, `PetscRegressorReset()`, `PetscRegressor`
345*34b254c5SRichard Tran Mills @*/
346*34b254c5SRichard Tran Mills PetscErrorCode PetscRegressorDestroy(PetscRegressor *regressor)
347*34b254c5SRichard Tran Mills {
348*34b254c5SRichard Tran Mills   PetscFunctionBegin;
349*34b254c5SRichard Tran Mills   if (!*regressor) PetscFunctionReturn(PETSC_SUCCESS);
350*34b254c5SRichard Tran Mills   PetscValidHeaderSpecific((*regressor), PETSCREGRESSOR_CLASSID, 1);
351*34b254c5SRichard Tran Mills   if (--((PetscObject)(*regressor))->refct > 0) {
352*34b254c5SRichard Tran Mills     *regressor = NULL;
353*34b254c5SRichard Tran Mills     PetscFunctionReturn(PETSC_SUCCESS);
354*34b254c5SRichard Tran Mills   }
355*34b254c5SRichard Tran Mills 
356*34b254c5SRichard Tran Mills   PetscCall(PetscRegressorReset(*regressor));
357*34b254c5SRichard Tran Mills   PetscTryTypeMethod(*regressor, destroy);
358*34b254c5SRichard Tran Mills 
359*34b254c5SRichard Tran Mills   PetscCall(PetscHeaderDestroy(regressor));
360*34b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
361*34b254c5SRichard Tran Mills }
362*34b254c5SRichard Tran Mills 
363*34b254c5SRichard Tran Mills /*@C
364*34b254c5SRichard Tran Mills   PetscRegressorSetType - Sets the type for the regressor.
365*34b254c5SRichard Tran Mills 
366*34b254c5SRichard Tran Mills   Collective
367*34b254c5SRichard Tran Mills 
368*34b254c5SRichard Tran Mills   Input Parameters:
369*34b254c5SRichard Tran Mills + regressor - the `PetscRegressor` context
370*34b254c5SRichard Tran Mills - type      - a known regression method
371*34b254c5SRichard Tran Mills 
372*34b254c5SRichard Tran Mills   Options Database Key:
373*34b254c5SRichard Tran Mills . -regressor_type <type> - Sets the type of regressor; use -help for a list of available types
374*34b254c5SRichard Tran Mills 
375*34b254c5SRichard Tran Mills   Level: intermediate
376*34b254c5SRichard Tran Mills 
377*34b254c5SRichard Tran Mills   Notes:
378*34b254c5SRichard Tran Mills   See "include/petscregressor.h" for available methods (for instance)
379*34b254c5SRichard Tran Mills .    `PETSCREGRESSORLINEAR` - Regression model that is linear in its coefficients; supports ordinary least squares as well as regularized variants
380*34b254c5SRichard Tran Mills 
381*34b254c5SRichard Tran Mills   Normally, it is best to use the `PetscRegressorSetFromOptions()` command and then
382*34b254c5SRichard Tran Mills   set the `PetscRegressor` type from the options database rather than by using
383*34b254c5SRichard Tran Mills   this routine, as this provides maximum flexibility.
384*34b254c5SRichard Tran Mills   The `PetscRegressorSetType()` routine is provided for those situations where it
385*34b254c5SRichard Tran Mills   is necessary to set the nonlinear solver independently of the command
386*34b254c5SRichard Tran Mills   line or options database.
387*34b254c5SRichard Tran Mills 
388*34b254c5SRichard Tran Mills .seealso: `PetscRegressorType`
389*34b254c5SRichard Tran Mills @*/
390*34b254c5SRichard Tran Mills PetscErrorCode PetscRegressorSetType(PetscRegressor regressor, PetscRegressorType type)
391*34b254c5SRichard Tran Mills {
392*34b254c5SRichard Tran Mills   PetscErrorCode (*r)(PetscRegressor);
393*34b254c5SRichard Tran Mills   PetscBool match;
394*34b254c5SRichard Tran Mills 
395*34b254c5SRichard Tran Mills   PetscFunctionBegin;
396*34b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
397*34b254c5SRichard Tran Mills   PetscAssertPointer(type, 2);
398*34b254c5SRichard Tran Mills 
399*34b254c5SRichard Tran Mills   PetscCall(PetscObjectTypeCompare((PetscObject)regressor, type, &match));
400*34b254c5SRichard Tran Mills   if (match) PetscFunctionReturn(PETSC_SUCCESS);
401*34b254c5SRichard Tran Mills 
402*34b254c5SRichard Tran Mills   PetscCall(PetscFunctionListFind(PetscRegressorList, type, &r));
403*34b254c5SRichard Tran Mills   PetscCheck(r, PetscObjectComm((PetscObject)regressor), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unable to find requested PetscRegressor type %s", type);
404*34b254c5SRichard Tran Mills 
405*34b254c5SRichard Tran Mills   /* Destroy the existing solver information */
406*34b254c5SRichard Tran Mills   PetscTryTypeMethod(regressor, destroy);
407*34b254c5SRichard Tran Mills   PetscCall(TaoDestroy(&regressor->tao));
408*34b254c5SRichard Tran Mills   regressor->ops->setup          = NULL;
409*34b254c5SRichard Tran Mills   regressor->ops->setfromoptions = NULL;
410*34b254c5SRichard Tran Mills   regressor->ops->settraining    = NULL;
411*34b254c5SRichard Tran Mills   regressor->ops->fit            = NULL;
412*34b254c5SRichard Tran Mills   regressor->ops->predict        = NULL;
413*34b254c5SRichard Tran Mills   regressor->ops->destroy        = NULL;
414*34b254c5SRichard Tran Mills   regressor->ops->reset          = NULL;
415*34b254c5SRichard Tran Mills   regressor->ops->view           = NULL;
416*34b254c5SRichard Tran Mills 
417*34b254c5SRichard Tran Mills   /* Call the PetscRegressorCreate_XXX routine for this particular regressor */
418*34b254c5SRichard Tran Mills   regressor->setupcalled = PETSC_FALSE;
419*34b254c5SRichard Tran Mills   PetscCall((*r)(regressor));
420*34b254c5SRichard Tran Mills   PetscCall(PetscObjectChangeTypeName((PetscObject)regressor, type));
421*34b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
422*34b254c5SRichard Tran Mills }
423*34b254c5SRichard Tran Mills 
424*34b254c5SRichard Tran Mills /*@
425*34b254c5SRichard Tran Mills   PetscRegressorGetType - Gets the current `PetscRegressorType` being used in the `PetscRegressor` object
426*34b254c5SRichard Tran Mills 
427*34b254c5SRichard Tran Mills   Not Collective
428*34b254c5SRichard Tran Mills 
429*34b254c5SRichard Tran Mills   Input Parameter:
430*34b254c5SRichard Tran Mills . regressor - the `PetscRegressor` solver context
431*34b254c5SRichard Tran Mills 
432*34b254c5SRichard Tran Mills   Output Parameter:
433*34b254c5SRichard Tran Mills . type - the `PetscRegressorType`
434*34b254c5SRichard Tran Mills 
435*34b254c5SRichard Tran Mills   Level: intermediate
436*34b254c5SRichard Tran Mills 
437*34b254c5SRichard Tran Mills .seealso: [](ch_regressor), `PetscRegressor`, `PetscRegressorType`, `PetscRegressorSetType()`
438*34b254c5SRichard Tran Mills @*/
439*34b254c5SRichard Tran Mills PetscErrorCode PetscRegressorGetType(PetscRegressor regressor, PetscRegressorType *type)
440*34b254c5SRichard Tran Mills {
441*34b254c5SRichard Tran Mills   PetscFunctionBegin;
442*34b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
443*34b254c5SRichard Tran Mills   PetscAssertPointer(type, 2);
444*34b254c5SRichard Tran Mills   *type = ((PetscObject)regressor)->type_name;
445*34b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
446*34b254c5SRichard Tran Mills }
447*34b254c5SRichard Tran Mills 
448*34b254c5SRichard Tran Mills /*@
449*34b254c5SRichard Tran Mills   PetscRegressorSetRegularizerWeight - Sets the weight to be used for the regularizer for a `PetscRegressor` context
450*34b254c5SRichard Tran Mills 
451*34b254c5SRichard Tran Mills   Logically Collective
452*34b254c5SRichard Tran Mills 
453*34b254c5SRichard Tran Mills   Input Parameters:
454*34b254c5SRichard Tran Mills + regressor - the `PetscRegressor` context
455*34b254c5SRichard Tran Mills - weight    - the regularizer weight
456*34b254c5SRichard Tran Mills 
457*34b254c5SRichard Tran Mills   Options Database Key:
458*34b254c5SRichard Tran Mills . regressor_regularizer_weight <weight> - sets the regularizer's weight
459*34b254c5SRichard Tran Mills 
460*34b254c5SRichard Tran Mills   Level: beginner
461*34b254c5SRichard Tran Mills 
462*34b254c5SRichard Tran Mills .seealso: `PetscRegressorSetType`
463*34b254c5SRichard Tran Mills @*/
464*34b254c5SRichard Tran Mills PetscErrorCode PetscRegressorSetRegularizerWeight(PetscRegressor regressor, PetscReal weight)
465*34b254c5SRichard Tran Mills {
466*34b254c5SRichard Tran Mills   PetscFunctionBegin;
467*34b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
468*34b254c5SRichard Tran Mills   PetscValidLogicalCollectiveReal(regressor, weight, 2);
469*34b254c5SRichard Tran Mills   regressor->regularizer_weight = weight;
470*34b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
471*34b254c5SRichard Tran Mills }
472*34b254c5SRichard Tran Mills 
473*34b254c5SRichard Tran Mills /*@
474*34b254c5SRichard Tran Mills   PetscRegressorGetTao - Returns the `Tao` context for a `PetscRegressor` object.
475*34b254c5SRichard Tran Mills 
476*34b254c5SRichard Tran Mills   Not Collective, but if the `PetscRegressor` is parallel, then the `Tao` object is parallel
477*34b254c5SRichard Tran Mills 
478*34b254c5SRichard Tran Mills   Input Parameter:
479*34b254c5SRichard Tran Mills . regressor - the regressor context
480*34b254c5SRichard Tran Mills 
481*34b254c5SRichard Tran Mills   Output Parameter:
482*34b254c5SRichard Tran Mills . tao - the `Tao` context
483*34b254c5SRichard Tran Mills 
484*34b254c5SRichard Tran Mills   Level: beginner
485*34b254c5SRichard Tran Mills 
486*34b254c5SRichard Tran Mills   Notes:
487*34b254c5SRichard Tran Mills   The `Tao` object will be created if it does not yet exist.
488*34b254c5SRichard Tran Mills 
489*34b254c5SRichard Tran Mills   The user can directly manipulate the `Tao` context to set various
490*34b254c5SRichard Tran Mills   options, etc.  Likewise, the user can then extract and manipulate the
491*34b254c5SRichard Tran Mills   child contexts such as `KSP` or `TaoLineSearch`as well.
492*34b254c5SRichard Tran Mills 
493*34b254c5SRichard Tran Mills   Depending on the type of the regressor and the options that are set, the regressor may use not use a `Tao` object.
494*34b254c5SRichard Tran Mills 
495*34b254c5SRichard Tran Mills .seealso: `PetscRegressorLinearGetKSP()`
496*34b254c5SRichard Tran Mills @*/
497*34b254c5SRichard Tran Mills PetscErrorCode PetscRegressorGetTao(PetscRegressor regressor, Tao *tao)
498*34b254c5SRichard Tran Mills {
499*34b254c5SRichard Tran Mills   PetscFunctionBegin;
500*34b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
501*34b254c5SRichard Tran Mills   PetscAssertPointer(tao, 2);
502*34b254c5SRichard Tran Mills   // Analogous to how SNESGetKSP() operates, this routine should create the Tao if it doesn't exist.
503*34b254c5SRichard Tran Mills   if (!regressor->tao) {
504*34b254c5SRichard Tran Mills     PetscCall(TaoCreate(PetscObjectComm((PetscObject)regressor), &regressor->tao));
505*34b254c5SRichard Tran Mills     PetscCall(PetscObjectIncrementTabLevel((PetscObject)regressor->tao, (PetscObject)regressor, 1));
506*34b254c5SRichard Tran Mills     PetscCall(PetscObjectSetOptions((PetscObject)regressor->tao, ((PetscObject)regressor)->options));
507*34b254c5SRichard Tran Mills   }
508*34b254c5SRichard Tran Mills   *tao = regressor->tao;
509*34b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
510*34b254c5SRichard Tran Mills }
511*34b254c5SRichard Tran Mills 
512*34b254c5SRichard Tran Mills /*@
513*34b254c5SRichard Tran Mills   PetscRegressorSetOptionsPrefix - Sets the prefix used for searching for all
514*34b254c5SRichard Tran Mills   PetscRegressor options in the database.
515*34b254c5SRichard Tran Mills 
516*34b254c5SRichard Tran Mills   Logically Collective
517*34b254c5SRichard Tran Mills 
518*34b254c5SRichard Tran Mills   Input Parameters:
519*34b254c5SRichard Tran Mills + regressor - the `PetscRegressor` context
520*34b254c5SRichard Tran Mills - p         - the prefix string to prepend to all PetscRegressor option requests
521*34b254c5SRichard Tran Mills 
522*34b254c5SRichard Tran Mills   Level: advanced
523*34b254c5SRichard Tran Mills 
524*34b254c5SRichard Tran Mills   Notes:
525*34b254c5SRichard Tran Mills   A hyphen (-) must NOT be given at the beginning of the prefix name.
526*34b254c5SRichard Tran Mills   The first character of all runtime options is AUTOMATICALLY the hyphen.
527*34b254c5SRichard Tran Mills 
528*34b254c5SRichard Tran Mills   For example, to distinguish between the runtime options for two
529*34b254c5SRichard Tran Mills   different PetscRegressor solvers, one could call
530*34b254c5SRichard Tran Mills .vb
531*34b254c5SRichard Tran Mills       PetscRegressorSetOptionsPrefix(regressor1,"sys1_")
532*34b254c5SRichard Tran Mills       PetscRegressorSetOptionsPrefix(regressor2,"sys2_")
533*34b254c5SRichard Tran Mills .ve
534*34b254c5SRichard Tran Mills 
535*34b254c5SRichard Tran Mills   This would enable use of different options for each system, such as
536*34b254c5SRichard Tran Mills .vb
537*34b254c5SRichard Tran Mills       -sys1_regressor_method linear -sys1_regressor_regularizer_weight 1.2
538*34b254c5SRichard Tran Mills       -sys2_regressor_method linear -sys2_regressor_regularizer_weight 1.1
539*34b254c5SRichard Tran Mills .ve
540*34b254c5SRichard Tran Mills 
541*34b254c5SRichard Tran Mills .seealso: [](ch_regressor), `PetscRegressor`, `PetscRegressorSetFromOptions()`, `PetscRegressorAppendOptionsPrefix()`, `PetscRegressorGetOptionsPrefix()`
542*34b254c5SRichard Tran Mills @*/
543*34b254c5SRichard Tran Mills PetscErrorCode PetscRegressorSetOptionsPrefix(PetscRegressor regressor, const char p[])
544*34b254c5SRichard Tran Mills {
545*34b254c5SRichard Tran Mills   PetscFunctionBegin;
546*34b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
547*34b254c5SRichard Tran Mills   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)regressor, p));
548*34b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
549*34b254c5SRichard Tran Mills }
550*34b254c5SRichard Tran Mills 
551*34b254c5SRichard Tran Mills /*@
552*34b254c5SRichard Tran Mills   PetscRegressorAppendOptionsPrefix - Appends to the prefix used for searching for all PetscRegressor options in the database.
553*34b254c5SRichard Tran Mills 
554*34b254c5SRichard Tran Mills   Logically Collective
555*34b254c5SRichard Tran Mills 
556*34b254c5SRichard Tran Mills   Input Parameters:
557*34b254c5SRichard Tran Mills + regressor - the `PetscRegressor` solver context
558*34b254c5SRichard Tran Mills - p         - the prefix string to prepend to all `PetscRegressor` option requests
559*34b254c5SRichard Tran Mills 
560*34b254c5SRichard Tran Mills   Level: advanced
561*34b254c5SRichard Tran Mills 
562*34b254c5SRichard Tran Mills   Note:
563*34b254c5SRichard Tran Mills   A hyphen (-) must NOT be given at the beginning of the prefix name.
564*34b254c5SRichard Tran Mills   The first character of all runtime options is automatically the hyphen.
565*34b254c5SRichard Tran Mills 
566*34b254c5SRichard Tran Mills .seealso: [](ch_regressor), `PetscRegressor`, `PetscRegressorSetFromOptions()`, `PetscRegressorSetOptionsPrefix()`, `PetscRegressorGetOptionsPrefix()`
567*34b254c5SRichard Tran Mills @*/
568*34b254c5SRichard Tran Mills PetscErrorCode PetscRegressorAppendOptionsPrefix(PetscRegressor regressor, const char p[])
569*34b254c5SRichard Tran Mills {
570*34b254c5SRichard Tran Mills   PetscFunctionBegin;
571*34b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
572*34b254c5SRichard Tran Mills   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)regressor, p));
573*34b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
574*34b254c5SRichard Tran Mills }
575*34b254c5SRichard Tran Mills 
576*34b254c5SRichard Tran Mills /*@
577*34b254c5SRichard Tran Mills   PetscRegressorGetOptionsPrefix - Gets the prefix used for searching for all
578*34b254c5SRichard Tran Mills   PetscRegressor options in the database
579*34b254c5SRichard Tran Mills 
580*34b254c5SRichard Tran Mills   Not Collective
581*34b254c5SRichard Tran Mills 
582*34b254c5SRichard Tran Mills   Input Parameter:
583*34b254c5SRichard Tran Mills . regressor - the `PetscRegressor` context
584*34b254c5SRichard Tran Mills 
585*34b254c5SRichard Tran Mills   Output Parameter:
586*34b254c5SRichard Tran Mills . p - pointer to the prefix string used is returned
587*34b254c5SRichard Tran Mills 
588*34b254c5SRichard Tran Mills   Fortran Notes:
589*34b254c5SRichard Tran Mills   Pass in a string 'prefix' of sufficient length to hold the prefix.
590*34b254c5SRichard Tran Mills 
591*34b254c5SRichard Tran Mills   Level: advanced
592*34b254c5SRichard Tran Mills 
593*34b254c5SRichard Tran Mills .seealso: [](ch_regressor), `PetscRegressor`, `PetscRegressorSetFromOptions()`, `PetscRegressorSetOptionsPrefix()`, `PetscRegressorAppendOptionsPrefix()`
594*34b254c5SRichard Tran Mills @*/
595*34b254c5SRichard Tran Mills PetscErrorCode PetscRegressorGetOptionsPrefix(PetscRegressor regressor, const char *p[])
596*34b254c5SRichard Tran Mills {
597*34b254c5SRichard Tran Mills   PetscFunctionBegin;
598*34b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
599*34b254c5SRichard Tran Mills   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)regressor, p));
600*34b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
601*34b254c5SRichard Tran Mills }
602