xref: /petsc/src/ml/regressor/interface/regressor.c (revision 834855d6effb0d027771461c8e947ee1ce5a1e17)
134b254c5SRichard Tran Mills #include <petsc/private/regressorimpl.h>
234b254c5SRichard Tran Mills 
334b254c5SRichard Tran Mills PetscBool         PetscRegressorRegisterAllCalled = PETSC_FALSE;
434b254c5SRichard Tran Mills PetscFunctionList PetscRegressorList              = NULL;
534b254c5SRichard Tran Mills 
634b254c5SRichard Tran Mills PetscClassId PETSCREGRESSOR_CLASSID;
734b254c5SRichard Tran Mills 
834b254c5SRichard Tran Mills /* Logging support */
934b254c5SRichard Tran Mills PetscLogEvent PetscRegressor_SetUp, PetscRegressor_Fit, PetscRegressor_Predict;
1034b254c5SRichard Tran Mills 
1134b254c5SRichard Tran Mills /*@C
1234b254c5SRichard Tran Mills   PetscRegressorRegister - Adds a method to the `PetscRegressor` package.
1334b254c5SRichard Tran Mills 
1434b254c5SRichard Tran Mills   Not collective
1534b254c5SRichard Tran Mills 
1634b254c5SRichard Tran Mills   Input Parameters:
1734b254c5SRichard Tran Mills + sname    - name of a new user-defined regressor
1834b254c5SRichard Tran Mills - function - routine to create method context
1934b254c5SRichard Tran Mills 
2034b254c5SRichard Tran Mills   Notes:
2134b254c5SRichard Tran Mills   `PetscRegressorRegister()` may be called multiple times to add several user-defined regressors.
2234b254c5SRichard Tran Mills 
2334b254c5SRichard Tran Mills   Example Usage:
2434b254c5SRichard Tran Mills .vb
2534b254c5SRichard Tran Mills    PetscRegressorRegister("my_regressor",MyRegressorCreate);
2634b254c5SRichard Tran Mills .ve
2734b254c5SRichard Tran Mills 
2834b254c5SRichard Tran Mills   Then, your regressor can be chosen with the procedural interface via
2934b254c5SRichard Tran Mills .vb
3034b254c5SRichard Tran Mills      PetscRegressorSetType(regressor,"my_regressor")
3134b254c5SRichard Tran Mills .ve
3234b254c5SRichard Tran Mills   or at runtime via the option
3334b254c5SRichard Tran Mills .vb
3434b254c5SRichard Tran Mills     -regressor_type my_regressor
3534b254c5SRichard Tran Mills .ve
3634b254c5SRichard Tran Mills 
3734b254c5SRichard Tran Mills   Level: advanced
3834b254c5SRichard Tran Mills 
3934b254c5SRichard Tran Mills .seealso: `PetscRegressorRegisterAll()`
4034b254c5SRichard Tran Mills @*/
PetscRegressorRegister(const char sname[],PetscErrorCode (* function)(PetscRegressor))4134b254c5SRichard Tran Mills PetscErrorCode PetscRegressorRegister(const char sname[], PetscErrorCode (*function)(PetscRegressor))
4234b254c5SRichard Tran Mills {
4334b254c5SRichard Tran Mills   PetscFunctionBegin;
4434b254c5SRichard Tran Mills   PetscCall(PetscRegressorInitializePackage());
4534b254c5SRichard Tran Mills   PetscCall(PetscFunctionListAdd(&PetscRegressorList, sname, function));
4634b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
4734b254c5SRichard Tran Mills }
4834b254c5SRichard Tran Mills 
4934b254c5SRichard Tran Mills /*@
5034b254c5SRichard Tran Mills   PetscRegressorCreate - Creates a `PetscRegressor` object.
5134b254c5SRichard Tran Mills 
5234b254c5SRichard Tran Mills   Collective
5334b254c5SRichard Tran Mills 
5434b254c5SRichard Tran Mills   Input Parameter:
5534b254c5SRichard Tran Mills . comm - the MPI communicator that will share the `PetscRegressor` object
5634b254c5SRichard Tran Mills 
5734b254c5SRichard Tran Mills   Output Parameter:
5834b254c5SRichard Tran Mills . newregressor - the new `PetscRegressor` object
5934b254c5SRichard Tran Mills 
6034b254c5SRichard Tran Mills   Level: beginner
6134b254c5SRichard Tran Mills 
6234b254c5SRichard Tran Mills .seealso: `PetscRegressorFit()`, `PetscRegressorPredict()`, `PetscRegressor`
6334b254c5SRichard Tran Mills @*/
PetscRegressorCreate(MPI_Comm comm,PetscRegressor * newregressor)6434b254c5SRichard Tran Mills PetscErrorCode PetscRegressorCreate(MPI_Comm comm, PetscRegressor *newregressor)
6534b254c5SRichard Tran Mills {
6634b254c5SRichard Tran Mills   PetscRegressor regressor;
6734b254c5SRichard Tran Mills 
6834b254c5SRichard Tran Mills   PetscFunctionBegin;
6934b254c5SRichard Tran Mills   PetscAssertPointer(newregressor, 2);
7034b254c5SRichard Tran Mills   *newregressor = NULL;
7134b254c5SRichard Tran Mills   PetscCall(PetscRegressorInitializePackage());
7234b254c5SRichard Tran Mills 
7334b254c5SRichard Tran Mills   PetscCall(PetscHeaderCreate(regressor, PETSCREGRESSOR_CLASSID, "PetscRegressor", "Regressor", "PetscRegressor", comm, PetscRegressorDestroy, PetscRegressorView));
7434b254c5SRichard Tran Mills 
7534b254c5SRichard Tran Mills   regressor->setupcalled = PETSC_FALSE;
7634b254c5SRichard Tran Mills   regressor->fitcalled   = PETSC_FALSE;
7734b254c5SRichard Tran Mills   regressor->data        = NULL;
7834b254c5SRichard Tran Mills   regressor->training    = NULL;
7934b254c5SRichard Tran Mills   regressor->target      = NULL;
8034b254c5SRichard Tran Mills   PetscObjectParameterSetDefault(regressor, regularizer_weight, 1.0); // Default to regularizer weight of 1.0, usually the default in SciKit-learn
8134b254c5SRichard Tran Mills 
8234b254c5SRichard Tran Mills   *newregressor = regressor;
8334b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
8434b254c5SRichard Tran Mills }
8534b254c5SRichard Tran Mills 
8634b254c5SRichard Tran Mills /*@
8734b254c5SRichard Tran Mills   PetscRegressorView - Prints information about the `PetscRegressor` object
8834b254c5SRichard Tran Mills 
8934b254c5SRichard Tran Mills   Collective
9034b254c5SRichard Tran Mills 
9134b254c5SRichard Tran Mills   Input Parameters:
9234b254c5SRichard Tran Mills + regressor - the `PetscRegressor` context
9334b254c5SRichard Tran Mills - viewer    - a `PetscViewer` context
9434b254c5SRichard Tran Mills 
9534b254c5SRichard Tran Mills   Options Database Key:
9634b254c5SRichard Tran Mills . -regressor_view - Calls `PetscRegressorView()` at the end of `PetscRegressorFit()`
9734b254c5SRichard Tran Mills 
9834b254c5SRichard Tran Mills   Level: beginner
9934b254c5SRichard Tran Mills 
10034b254c5SRichard Tran Mills   Notes:
10134b254c5SRichard Tran Mills   The available visualization contexts include
10234b254c5SRichard Tran Mills +     `PETSC_VIEWER_STDOUT_SELF` - standard output (default)
10334b254c5SRichard Tran Mills -     `PETSC_VIEWER_STDOUT_WORLD` - synchronized standard
10434b254c5SRichard Tran Mills   output where only the first processor opens
10534b254c5SRichard Tran Mills   the file.  All other processors send their
10634b254c5SRichard Tran Mills   data to the first processor to print.
10734b254c5SRichard Tran Mills 
10834b254c5SRichard Tran Mills .seealso: [](ch_regressor), `PetscRegressor`, `PetscViewerASCIIOpen()`
10934b254c5SRichard Tran Mills @*/
PetscRegressorView(PetscRegressor regressor,PetscViewer viewer)11034b254c5SRichard Tran Mills PetscErrorCode PetscRegressorView(PetscRegressor regressor, PetscViewer viewer)
11134b254c5SRichard Tran Mills {
11234b254c5SRichard Tran Mills   PetscBool          isascii, isstring;
11334b254c5SRichard Tran Mills   PetscRegressorType type;
11434b254c5SRichard Tran Mills 
11534b254c5SRichard Tran Mills   PetscFunctionBegin;
11634b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
11734b254c5SRichard Tran Mills   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(((PetscObject)regressor)->comm, &viewer));
11834b254c5SRichard Tran Mills   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
11934b254c5SRichard Tran Mills   PetscCheckSameComm(regressor, 1, viewer, 2);
12034b254c5SRichard Tran Mills 
12134b254c5SRichard Tran Mills   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
12234b254c5SRichard Tran Mills   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring));
12334b254c5SRichard Tran Mills   if (isascii) {
12434b254c5SRichard Tran Mills     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)regressor, viewer));
12534b254c5SRichard Tran Mills 
12634b254c5SRichard Tran Mills     PetscCall(PetscViewerASCIIPushTab(viewer));
12734b254c5SRichard Tran Mills     PetscTryTypeMethod(regressor, view, viewer);
12834b254c5SRichard Tran Mills     if (regressor->tao) PetscCall(TaoView(regressor->tao, viewer));
12934b254c5SRichard Tran Mills     PetscCall(PetscViewerASCIIPopTab(viewer));
13034b254c5SRichard Tran Mills   } else if (isstring) {
13134b254c5SRichard Tran Mills     PetscCall(PetscRegressorGetType(regressor, &type));
13234b254c5SRichard Tran Mills     PetscCall(PetscViewerStringSPrintf(viewer, " PetscRegressorType: %-7.7s", type));
13334b254c5SRichard Tran Mills   }
13434b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
13534b254c5SRichard Tran Mills }
13634b254c5SRichard Tran Mills 
13734b254c5SRichard Tran Mills /*@
13834b254c5SRichard Tran Mills   PetscRegressorViewFromOptions - View a `PetscRegressor` object based on values in the options database
13934b254c5SRichard Tran Mills 
14034b254c5SRichard Tran Mills   Collective
14134b254c5SRichard Tran Mills 
14234b254c5SRichard Tran Mills   Input Parameters:
14334b254c5SRichard Tran Mills + A    - the  `PetscRegressor` context
14434b254c5SRichard Tran Mills . obj  - Optional object that provides the prefix for the options database
14534b254c5SRichard Tran Mills - name - command line option
14634b254c5SRichard Tran Mills 
14734b254c5SRichard Tran Mills   Level: intermediate
14834b254c5SRichard Tran Mills 
14934b254c5SRichard Tran Mills .seealso: [](ch_regressor), `PetscRegressor`, `PetscRegressorView`, `PetscObjectViewFromOptions()`, `PetscRegressorCreate()`
15034b254c5SRichard Tran Mills @*/
PetscRegressorViewFromOptions(PetscRegressor A,PetscObject obj,const char name[])15134b254c5SRichard Tran Mills PetscErrorCode PetscRegressorViewFromOptions(PetscRegressor A, PetscObject obj, const char name[])
15234b254c5SRichard Tran Mills {
15334b254c5SRichard Tran Mills   PetscFunctionBegin;
15434b254c5SRichard Tran Mills   PetscValidHeaderSpecific(A, PETSCREGRESSOR_CLASSID, 1);
15534b254c5SRichard Tran Mills   PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
15634b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
15734b254c5SRichard Tran Mills }
15834b254c5SRichard Tran Mills 
15934b254c5SRichard Tran Mills /*@
16034b254c5SRichard Tran Mills   PetscRegressorSetFromOptions - Sets `PetscRegressor` options from the options database.
16134b254c5SRichard Tran Mills 
16234b254c5SRichard Tran Mills   Collective
16334b254c5SRichard Tran Mills 
16434b254c5SRichard Tran Mills   Input Parameter:
16534b254c5SRichard Tran Mills . regressor - the `PetscRegressor` context
16634b254c5SRichard Tran Mills 
16734b254c5SRichard Tran Mills   Options Database Keys:
16834b254c5SRichard Tran Mills . -regressor_type <type> - the particular type of regressor to be used; see `PetscRegressorType` for complete list
16934b254c5SRichard Tran Mills 
17034b254c5SRichard Tran Mills   Level: beginner
17134b254c5SRichard Tran Mills 
17234b254c5SRichard Tran Mills   Note:
17334b254c5SRichard Tran Mills   This routine must be called before `PetscRegressorSetUp()` (or `PetscRegressorFit()`, which calls
17434b254c5SRichard Tran Mills   the former) if the user is to be allowed to set the regressor type.
17534b254c5SRichard Tran Mills 
17634b254c5SRichard Tran Mills .seealso: `PetscRegressor`, `PetscRegressorCreate()`
17734b254c5SRichard Tran Mills @*/
PetscRegressorSetFromOptions(PetscRegressor regressor)17834b254c5SRichard Tran Mills PetscErrorCode PetscRegressorSetFromOptions(PetscRegressor regressor)
17934b254c5SRichard Tran Mills {
18034b254c5SRichard Tran Mills   PetscBool          flg;
18134b254c5SRichard Tran Mills   PetscRegressorType default_type = PETSCREGRESSORLINEAR;
18234b254c5SRichard Tran Mills   char               type[256];
18334b254c5SRichard Tran Mills 
18434b254c5SRichard Tran Mills   PetscFunctionBegin;
18534b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
18634b254c5SRichard Tran Mills   if (((PetscObject)regressor)->type_name) default_type = ((PetscObject)regressor)->type_name;
18734b254c5SRichard Tran Mills   PetscObjectOptionsBegin((PetscObject)regressor);
18834b254c5SRichard Tran Mills   /* Check for type from options */
18934b254c5SRichard Tran Mills   PetscCall(PetscOptionsFList("-regressor_type", "PetscRegressor type", "PetscRegressorSetType", PetscRegressorList, default_type, type, 256, &flg));
19034b254c5SRichard Tran Mills   if (flg) {
19134b254c5SRichard Tran Mills     PetscCall(PetscRegressorSetType(regressor, type));
19234b254c5SRichard Tran Mills   } else if (!((PetscObject)regressor)->type_name) {
19334b254c5SRichard Tran Mills     PetscCall(PetscRegressorSetType(regressor, default_type));
19434b254c5SRichard Tran Mills   }
19534b254c5SRichard Tran Mills   PetscCall(PetscOptionsReal("-regressor_regularizer_weight", "Weight for the regularizer", "PetscRegressorSetRegularizerWeight", regressor->regularizer_weight, &regressor->regularizer_weight, &flg));
19634b254c5SRichard Tran Mills   if (flg) PetscCall(PetscRegressorSetRegularizerWeight(regressor, regressor->regularizer_weight));
19734b254c5SRichard 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!
19834b254c5SRichard Tran Mills   PetscTryTypeMethod(regressor, setfromoptions, PetscOptionsObject);
19934b254c5SRichard Tran Mills   PetscOptionsEnd();
20034b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
20134b254c5SRichard Tran Mills }
20234b254c5SRichard Tran Mills 
20334b254c5SRichard Tran Mills /*@
20434b254c5SRichard Tran Mills   PetscRegressorSetUp - Sets up the internal data structures for the later use of a regressor.
20534b254c5SRichard Tran Mills 
20634b254c5SRichard Tran Mills   Collective
20734b254c5SRichard Tran Mills 
20834b254c5SRichard Tran Mills   Input Parameter:
20934b254c5SRichard Tran Mills . regressor - the `PetscRegressor` context
21034b254c5SRichard Tran Mills 
21134b254c5SRichard Tran Mills   Notes:
21234b254c5SRichard Tran Mills   For basic use of the `PetscRegressor` solvers the user need not to explicitly call
21334b254c5SRichard Tran Mills   `PetscRegressorSetUp()`, since these actions will automatically occur during
21434b254c5SRichard Tran Mills   the call to `PetscRegressorFit()`.  However, if one wishes to control this
21534b254c5SRichard Tran Mills   phase separately, `PetscRegressorSetUp()` should be called after `PetscRegressorCreate()`,
21634b254c5SRichard Tran Mills   `PetscRegressorSetUp()`, and optional routines of the form `PetscRegressorSetXXX()`,
21734b254c5SRichard Tran Mills   but before `PetscRegressorFit()`.
21834b254c5SRichard Tran Mills 
21934b254c5SRichard Tran Mills   Level: advanced
22034b254c5SRichard Tran Mills 
22134b254c5SRichard Tran Mills .seealso: `PetscRegressorCreate()`, `PetscRegressorFit()`, `PetscRegressorDestroy()`
22234b254c5SRichard Tran Mills @*/
PetscRegressorSetUp(PetscRegressor regressor)22334b254c5SRichard Tran Mills PetscErrorCode PetscRegressorSetUp(PetscRegressor regressor)
22434b254c5SRichard Tran Mills {
22534b254c5SRichard Tran Mills   PetscFunctionBegin;
22634b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
22734b254c5SRichard Tran Mills   if (regressor->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
22834b254c5SRichard Tran Mills   PetscCall(PetscLogEventBegin(PetscRegressor_SetUp, regressor, 0, 0, 0));
22934b254c5SRichard Tran Mills   //TODO is there some mat vec etc that must be set, like TaoSolution?
23034b254c5SRichard Tran Mills   PetscTryTypeMethod(regressor, setup);
23134b254c5SRichard Tran Mills   regressor->setupcalled = PETSC_TRUE;
23234b254c5SRichard Tran Mills   PetscCall(PetscLogEventEnd(PetscRegressor_SetUp, regressor, 0, 0, 0));
23334b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
23434b254c5SRichard Tran Mills }
23534b254c5SRichard Tran Mills 
23634b254c5SRichard Tran Mills /* NOTE: I've decided to make this take X and y, like the Scikit-learn Fit routines do.
23734b254c5SRichard Tran Mills  * Am I overlooking some reason that X should be set in a separate function call, a la KSPSetOperators()?. */
23834b254c5SRichard Tran Mills /*@
23934b254c5SRichard Tran Mills   PetscRegressorFit - Fit, or train, a regressor from a training dataset
24034b254c5SRichard Tran Mills 
24134b254c5SRichard Tran Mills   Collective
24234b254c5SRichard Tran Mills 
24334b254c5SRichard Tran Mills   Input Parameters:
24434b254c5SRichard Tran Mills + regressor - the `PetscRegressor` context
24534b254c5SRichard Tran Mills . X         - matrix of training data (of dimension [number of samples] x [number of features])
24634b254c5SRichard Tran Mills - y         - vector of target values from the training dataset
24734b254c5SRichard Tran Mills 
24834b254c5SRichard Tran Mills   Level: beginner
24934b254c5SRichard Tran Mills 
25034b254c5SRichard Tran Mills .seealso: `PetscRegressorCreate()`, `PetscRegressorSetUp()`, `PetscRegressorDestroy()`, `PetscRegressorPredict()`
25134b254c5SRichard Tran Mills @*/
PetscRegressorFit(PetscRegressor regressor,Mat X,Vec y)25234b254c5SRichard Tran Mills PetscErrorCode PetscRegressorFit(PetscRegressor regressor, Mat X, Vec y)
25334b254c5SRichard Tran Mills {
25434b254c5SRichard Tran Mills   PetscFunctionBegin;
25534b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
25634b254c5SRichard Tran Mills   if (X) PetscValidHeaderSpecific(X, MAT_CLASSID, 2);
25734b254c5SRichard Tran Mills   if (y) PetscValidHeaderSpecific(y, VEC_CLASSID, 3);
25834b254c5SRichard Tran Mills 
25934b254c5SRichard Tran Mills   if (X) {
26034b254c5SRichard Tran Mills     PetscCall(PetscObjectReference((PetscObject)X));
26134b254c5SRichard Tran Mills     PetscCall(MatDestroy(&regressor->training));
26234b254c5SRichard Tran Mills     regressor->training = X;
26334b254c5SRichard Tran Mills   }
26434b254c5SRichard Tran Mills   if (y) {
26534b254c5SRichard Tran Mills     PetscCall(PetscObjectReference((PetscObject)y));
26634b254c5SRichard Tran Mills     PetscCall(VecDestroy(&regressor->target));
26734b254c5SRichard Tran Mills     regressor->target = y;
26834b254c5SRichard Tran Mills   }
26934b254c5SRichard Tran Mills   PetscCall(PetscRegressorSetUp(regressor));
27034b254c5SRichard Tran Mills 
27134b254c5SRichard Tran Mills   PetscCall(PetscLogEventBegin(PetscRegressor_Fit, regressor, X, y, 0));
27234b254c5SRichard Tran Mills   PetscUseTypeMethod(regressor, fit);
27334b254c5SRichard Tran Mills   PetscCall(PetscLogEventEnd(PetscRegressor_Fit, regressor, X, y, 0));
27434b254c5SRichard Tran Mills   //TODO print convergence data
27534b254c5SRichard Tran Mills   PetscCall(PetscRegressorViewFromOptions(regressor, NULL, "-regressor_view"));
27634b254c5SRichard Tran Mills   regressor->fitcalled = PETSC_TRUE;
27734b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
27834b254c5SRichard Tran Mills }
27934b254c5SRichard Tran Mills 
28034b254c5SRichard Tran Mills /*@
28134b254c5SRichard Tran Mills   PetscRegressorPredict - Compute predictions (that is, perform inference) using a fitted regression model.
28234b254c5SRichard Tran Mills 
28334b254c5SRichard Tran Mills   Collective
28434b254c5SRichard Tran Mills 
28534b254c5SRichard Tran Mills   Input Parameters:
28634b254c5SRichard Tran Mills + regressor - the `PetscRegressor` context (for which `PetscRegressorFit()` must have been called)
28734b254c5SRichard Tran Mills - X         - data matrix of unlabeled observations
28834b254c5SRichard Tran Mills 
28934b254c5SRichard Tran Mills   Output Parameter:
29034b254c5SRichard Tran Mills . y - vector of predicted labels
29134b254c5SRichard Tran Mills 
29234b254c5SRichard Tran Mills   Level: beginner
29334b254c5SRichard Tran Mills 
29434b254c5SRichard Tran Mills .seealso: `PetscRegressorFit()`, `PetscRegressorDestroy()`
29534b254c5SRichard Tran Mills @*/
PetscRegressorPredict(PetscRegressor regressor,Mat X,Vec y)29634b254c5SRichard Tran Mills PetscErrorCode PetscRegressorPredict(PetscRegressor regressor, Mat X, Vec y)
29734b254c5SRichard Tran Mills {
29834b254c5SRichard Tran Mills   PetscFunctionBegin;
29934b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
30034b254c5SRichard Tran Mills   if (X) PetscValidHeaderSpecific(X, MAT_CLASSID, 2);
30134b254c5SRichard Tran Mills   if (y) PetscValidHeaderSpecific(y, VEC_CLASSID, 3);
30234b254c5SRichard Tran Mills   PetscCheck(regressor->fitcalled == PETSC_TRUE, ((PetscObject)regressor)->comm, PETSC_ERR_ARG_WRONGSTATE, "PetscRegressorFit() must be called before PetscRegressorPredict()");
30334b254c5SRichard Tran Mills   PetscCall(PetscLogEventBegin(PetscRegressor_Predict, regressor, X, y, 0));
30434b254c5SRichard Tran Mills   PetscTryTypeMethod(regressor, predict, X, y);
30534b254c5SRichard Tran Mills   PetscCall(PetscLogEventEnd(PetscRegressor_Predict, regressor, X, y, 0));
30634b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
30734b254c5SRichard Tran Mills }
30834b254c5SRichard Tran Mills 
30934b254c5SRichard Tran Mills /*@
31034b254c5SRichard Tran Mills   PetscRegressorReset - Resets a `PetscRegressor` context by removing any allocated `Vec` and `Mat`. Any options set in the object remain.
31134b254c5SRichard Tran Mills 
31234b254c5SRichard Tran Mills   Collective
31334b254c5SRichard Tran Mills 
31434b254c5SRichard Tran Mills   Input Parameter:
31534b254c5SRichard Tran Mills . regressor - context obtained from `PetscRegressorCreate()`
31634b254c5SRichard Tran Mills 
31734b254c5SRichard Tran Mills   Level: intermediate
31834b254c5SRichard Tran Mills 
31934b254c5SRichard Tran Mills .seealso: `PetscRegressorCreate()`, `PetscRegressorSetUp()`, `PetscRegressorFit()`, `PetscRegressorPredict()`, `PetscRegressorDestroy()`
32034b254c5SRichard Tran Mills @*/
PetscRegressorReset(PetscRegressor regressor)32134b254c5SRichard Tran Mills PetscErrorCode PetscRegressorReset(PetscRegressor regressor)
32234b254c5SRichard Tran Mills {
32334b254c5SRichard Tran Mills   PetscFunctionBegin;
32434b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
32534b254c5SRichard Tran Mills   if (regressor->ops->reset) PetscTryTypeMethod(regressor, reset);
32634b254c5SRichard Tran Mills   PetscCall(MatDestroy(&regressor->training));
32734b254c5SRichard Tran Mills   PetscCall(VecDestroy(&regressor->target));
32834b254c5SRichard Tran Mills   PetscCall(TaoDestroy(&regressor->tao));
32934b254c5SRichard Tran Mills   regressor->setupcalled = PETSC_FALSE;
33034b254c5SRichard Tran Mills   regressor->fitcalled   = PETSC_FALSE;
33134b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
33234b254c5SRichard Tran Mills }
33334b254c5SRichard Tran Mills 
33434b254c5SRichard Tran Mills /*@C
33534b254c5SRichard Tran Mills   PetscRegressorDestroy - Destroys the regressor context that was created with `PetscRegressorCreate()`.
33634b254c5SRichard Tran Mills 
33734b254c5SRichard Tran Mills   Collective
33834b254c5SRichard Tran Mills 
33934b254c5SRichard Tran Mills   Input Parameter:
34034b254c5SRichard Tran Mills . regressor - the `PetscRegressor` context
34134b254c5SRichard Tran Mills 
34234b254c5SRichard Tran Mills   Level: beginner
34334b254c5SRichard Tran Mills 
34434b254c5SRichard Tran Mills .seealso: `PetscRegressorCreate()`, `PetscRegressorSetUp()`, `PetscRegressorReset()`, `PetscRegressor`
34534b254c5SRichard Tran Mills @*/
PetscRegressorDestroy(PetscRegressor * regressor)34634b254c5SRichard Tran Mills PetscErrorCode PetscRegressorDestroy(PetscRegressor *regressor)
34734b254c5SRichard Tran Mills {
34834b254c5SRichard Tran Mills   PetscFunctionBegin;
34934b254c5SRichard Tran Mills   if (!*regressor) PetscFunctionReturn(PETSC_SUCCESS);
350*3a7d0413SPierre Jolivet   PetscValidHeaderSpecific(*regressor, PETSCREGRESSOR_CLASSID, 1);
351*3a7d0413SPierre Jolivet   if (--((PetscObject)*regressor)->refct > 0) {
35234b254c5SRichard Tran Mills     *regressor = NULL;
35334b254c5SRichard Tran Mills     PetscFunctionReturn(PETSC_SUCCESS);
35434b254c5SRichard Tran Mills   }
35534b254c5SRichard Tran Mills 
35634b254c5SRichard Tran Mills   PetscCall(PetscRegressorReset(*regressor));
35734b254c5SRichard Tran Mills   PetscTryTypeMethod(*regressor, destroy);
35834b254c5SRichard Tran Mills 
35934b254c5SRichard Tran Mills   PetscCall(PetscHeaderDestroy(regressor));
36034b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
36134b254c5SRichard Tran Mills }
36234b254c5SRichard Tran Mills 
36334b254c5SRichard Tran Mills /*@C
36434b254c5SRichard Tran Mills   PetscRegressorSetType - Sets the type for the regressor.
36534b254c5SRichard Tran Mills 
36634b254c5SRichard Tran Mills   Collective
36734b254c5SRichard Tran Mills 
36834b254c5SRichard Tran Mills   Input Parameters:
36934b254c5SRichard Tran Mills + regressor - the `PetscRegressor` context
37034b254c5SRichard Tran Mills - type      - a known regression method
37134b254c5SRichard Tran Mills 
37234b254c5SRichard Tran Mills   Options Database Key:
37334b254c5SRichard Tran Mills . -regressor_type <type> - Sets the type of regressor; use -help for a list of available types
37434b254c5SRichard Tran Mills 
37534b254c5SRichard Tran Mills   Level: intermediate
37634b254c5SRichard Tran Mills 
37734b254c5SRichard Tran Mills   Notes:
37834b254c5SRichard Tran Mills   See "include/petscregressor.h" for available methods (for instance)
37934b254c5SRichard Tran Mills .    `PETSCREGRESSORLINEAR` - Regression model that is linear in its coefficients; supports ordinary least squares as well as regularized variants
38034b254c5SRichard Tran Mills 
38134b254c5SRichard Tran Mills   Normally, it is best to use the `PetscRegressorSetFromOptions()` command and then
38234b254c5SRichard Tran Mills   set the `PetscRegressor` type from the options database rather than by using
38334b254c5SRichard Tran Mills   this routine, as this provides maximum flexibility.
38434b254c5SRichard Tran Mills   The `PetscRegressorSetType()` routine is provided for those situations where it
38534b254c5SRichard Tran Mills   is necessary to set the nonlinear solver independently of the command
38634b254c5SRichard Tran Mills   line or options database.
38734b254c5SRichard Tran Mills 
38834b254c5SRichard Tran Mills .seealso: `PetscRegressorType`
38934b254c5SRichard Tran Mills @*/
PetscRegressorSetType(PetscRegressor regressor,PetscRegressorType type)39034b254c5SRichard Tran Mills PetscErrorCode PetscRegressorSetType(PetscRegressor regressor, PetscRegressorType type)
39134b254c5SRichard Tran Mills {
39234b254c5SRichard Tran Mills   PetscErrorCode (*r)(PetscRegressor);
39334b254c5SRichard Tran Mills   PetscBool match;
39434b254c5SRichard Tran Mills 
39534b254c5SRichard Tran Mills   PetscFunctionBegin;
39634b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
39734b254c5SRichard Tran Mills   PetscAssertPointer(type, 2);
39834b254c5SRichard Tran Mills 
39934b254c5SRichard Tran Mills   PetscCall(PetscObjectTypeCompare((PetscObject)regressor, type, &match));
40034b254c5SRichard Tran Mills   if (match) PetscFunctionReturn(PETSC_SUCCESS);
40134b254c5SRichard Tran Mills 
40234b254c5SRichard Tran Mills   PetscCall(PetscFunctionListFind(PetscRegressorList, type, &r));
40334b254c5SRichard Tran Mills   PetscCheck(r, PetscObjectComm((PetscObject)regressor), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unable to find requested PetscRegressor type %s", type);
40434b254c5SRichard Tran Mills 
40534b254c5SRichard Tran Mills   /* Destroy the existing solver information */
40634b254c5SRichard Tran Mills   PetscTryTypeMethod(regressor, destroy);
40734b254c5SRichard Tran Mills   PetscCall(TaoDestroy(&regressor->tao));
40834b254c5SRichard Tran Mills   regressor->ops->setup          = NULL;
40934b254c5SRichard Tran Mills   regressor->ops->setfromoptions = NULL;
41034b254c5SRichard Tran Mills   regressor->ops->settraining    = NULL;
41134b254c5SRichard Tran Mills   regressor->ops->fit            = NULL;
41234b254c5SRichard Tran Mills   regressor->ops->predict        = NULL;
41334b254c5SRichard Tran Mills   regressor->ops->destroy        = NULL;
41434b254c5SRichard Tran Mills   regressor->ops->reset          = NULL;
41534b254c5SRichard Tran Mills   regressor->ops->view           = NULL;
41634b254c5SRichard Tran Mills 
41734b254c5SRichard Tran Mills   /* Call the PetscRegressorCreate_XXX routine for this particular regressor */
41834b254c5SRichard Tran Mills   regressor->setupcalled = PETSC_FALSE;
41934b254c5SRichard Tran Mills   PetscCall((*r)(regressor));
42034b254c5SRichard Tran Mills   PetscCall(PetscObjectChangeTypeName((PetscObject)regressor, type));
42134b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
42234b254c5SRichard Tran Mills }
42334b254c5SRichard Tran Mills 
42434b254c5SRichard Tran Mills /*@
42534b254c5SRichard Tran Mills   PetscRegressorGetType - Gets the current `PetscRegressorType` being used in the `PetscRegressor` object
42634b254c5SRichard Tran Mills 
42734b254c5SRichard Tran Mills   Not Collective
42834b254c5SRichard Tran Mills 
42934b254c5SRichard Tran Mills   Input Parameter:
43034b254c5SRichard Tran Mills . regressor - the `PetscRegressor` solver context
43134b254c5SRichard Tran Mills 
43234b254c5SRichard Tran Mills   Output Parameter:
43334b254c5SRichard Tran Mills . type - the `PetscRegressorType`
43434b254c5SRichard Tran Mills 
43534b254c5SRichard Tran Mills   Level: intermediate
43634b254c5SRichard Tran Mills 
43734b254c5SRichard Tran Mills .seealso: [](ch_regressor), `PetscRegressor`, `PetscRegressorType`, `PetscRegressorSetType()`
43834b254c5SRichard Tran Mills @*/
PetscRegressorGetType(PetscRegressor regressor,PetscRegressorType * type)43934b254c5SRichard Tran Mills PetscErrorCode PetscRegressorGetType(PetscRegressor regressor, PetscRegressorType *type)
44034b254c5SRichard Tran Mills {
44134b254c5SRichard Tran Mills   PetscFunctionBegin;
44234b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
44334b254c5SRichard Tran Mills   PetscAssertPointer(type, 2);
44434b254c5SRichard Tran Mills   *type = ((PetscObject)regressor)->type_name;
44534b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
44634b254c5SRichard Tran Mills }
44734b254c5SRichard Tran Mills 
44834b254c5SRichard Tran Mills /*@
44934b254c5SRichard Tran Mills   PetscRegressorSetRegularizerWeight - Sets the weight to be used for the regularizer for a `PetscRegressor` context
45034b254c5SRichard Tran Mills 
45134b254c5SRichard Tran Mills   Logically Collective
45234b254c5SRichard Tran Mills 
45334b254c5SRichard Tran Mills   Input Parameters:
45434b254c5SRichard Tran Mills + regressor - the `PetscRegressor` context
45534b254c5SRichard Tran Mills - weight    - the regularizer weight
45634b254c5SRichard Tran Mills 
45734b254c5SRichard Tran Mills   Options Database Key:
45834b254c5SRichard Tran Mills . regressor_regularizer_weight <weight> - sets the regularizer's weight
45934b254c5SRichard Tran Mills 
46034b254c5SRichard Tran Mills   Level: beginner
46134b254c5SRichard Tran Mills 
46234b254c5SRichard Tran Mills .seealso: `PetscRegressorSetType`
46334b254c5SRichard Tran Mills @*/
PetscRegressorSetRegularizerWeight(PetscRegressor regressor,PetscReal weight)46434b254c5SRichard Tran Mills PetscErrorCode PetscRegressorSetRegularizerWeight(PetscRegressor regressor, PetscReal weight)
46534b254c5SRichard Tran Mills {
46634b254c5SRichard Tran Mills   PetscFunctionBegin;
46734b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
46834b254c5SRichard Tran Mills   PetscValidLogicalCollectiveReal(regressor, weight, 2);
46934b254c5SRichard Tran Mills   regressor->regularizer_weight = weight;
47034b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
47134b254c5SRichard Tran Mills }
47234b254c5SRichard Tran Mills 
47334b254c5SRichard Tran Mills /*@
47434b254c5SRichard Tran Mills   PetscRegressorGetTao - Returns the `Tao` context for a `PetscRegressor` object.
47534b254c5SRichard Tran Mills 
47634b254c5SRichard Tran Mills   Not Collective, but if the `PetscRegressor` is parallel, then the `Tao` object is parallel
47734b254c5SRichard Tran Mills 
47834b254c5SRichard Tran Mills   Input Parameter:
47934b254c5SRichard Tran Mills . regressor - the regressor context
48034b254c5SRichard Tran Mills 
48134b254c5SRichard Tran Mills   Output Parameter:
48234b254c5SRichard Tran Mills . tao - the `Tao` context
48334b254c5SRichard Tran Mills 
48434b254c5SRichard Tran Mills   Level: beginner
48534b254c5SRichard Tran Mills 
48634b254c5SRichard Tran Mills   Notes:
48734b254c5SRichard Tran Mills   The `Tao` object will be created if it does not yet exist.
48834b254c5SRichard Tran Mills 
48934b254c5SRichard Tran Mills   The user can directly manipulate the `Tao` context to set various
49034b254c5SRichard Tran Mills   options, etc.  Likewise, the user can then extract and manipulate the
49134b254c5SRichard Tran Mills   child contexts such as `KSP` or `TaoLineSearch`as well.
49234b254c5SRichard Tran Mills 
49334b254c5SRichard Tran Mills   Depending on the type of the regressor and the options that are set, the regressor may use not use a `Tao` object.
49434b254c5SRichard Tran Mills 
49534b254c5SRichard Tran Mills .seealso: `PetscRegressorLinearGetKSP()`
49634b254c5SRichard Tran Mills @*/
PetscRegressorGetTao(PetscRegressor regressor,Tao * tao)49734b254c5SRichard Tran Mills PetscErrorCode PetscRegressorGetTao(PetscRegressor regressor, Tao *tao)
49834b254c5SRichard Tran Mills {
49934b254c5SRichard Tran Mills   PetscFunctionBegin;
50034b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
50134b254c5SRichard Tran Mills   PetscAssertPointer(tao, 2);
50234b254c5SRichard Tran Mills   // Analogous to how SNESGetKSP() operates, this routine should create the Tao if it doesn't exist.
50334b254c5SRichard Tran Mills   if (!regressor->tao) {
50434b254c5SRichard Tran Mills     PetscCall(TaoCreate(PetscObjectComm((PetscObject)regressor), &regressor->tao));
50534b254c5SRichard Tran Mills     PetscCall(PetscObjectIncrementTabLevel((PetscObject)regressor->tao, (PetscObject)regressor, 1));
50634b254c5SRichard Tran Mills     PetscCall(PetscObjectSetOptions((PetscObject)regressor->tao, ((PetscObject)regressor)->options));
50734b254c5SRichard Tran Mills   }
50834b254c5SRichard Tran Mills   *tao = regressor->tao;
50934b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
51034b254c5SRichard Tran Mills }
51134b254c5SRichard Tran Mills 
51234b254c5SRichard Tran Mills /*@
51334b254c5SRichard Tran Mills   PetscRegressorSetOptionsPrefix - Sets the prefix used for searching for all
51434b254c5SRichard Tran Mills   PetscRegressor options in the database.
51534b254c5SRichard Tran Mills 
51634b254c5SRichard Tran Mills   Logically Collective
51734b254c5SRichard Tran Mills 
51834b254c5SRichard Tran Mills   Input Parameters:
51934b254c5SRichard Tran Mills + regressor - the `PetscRegressor` context
52034b254c5SRichard Tran Mills - p         - the prefix string to prepend to all PetscRegressor option requests
52134b254c5SRichard Tran Mills 
52234b254c5SRichard Tran Mills   Level: advanced
52334b254c5SRichard Tran Mills 
52434b254c5SRichard Tran Mills   Notes:
52534b254c5SRichard Tran Mills   A hyphen (-) must NOT be given at the beginning of the prefix name.
52634b254c5SRichard Tran Mills   The first character of all runtime options is AUTOMATICALLY the hyphen.
52734b254c5SRichard Tran Mills 
52834b254c5SRichard Tran Mills   For example, to distinguish between the runtime options for two
52934b254c5SRichard Tran Mills   different PetscRegressor solvers, one could call
53034b254c5SRichard Tran Mills .vb
53134b254c5SRichard Tran Mills       PetscRegressorSetOptionsPrefix(regressor1,"sys1_")
53234b254c5SRichard Tran Mills       PetscRegressorSetOptionsPrefix(regressor2,"sys2_")
53334b254c5SRichard Tran Mills .ve
53434b254c5SRichard Tran Mills 
53534b254c5SRichard Tran Mills   This would enable use of different options for each system, such as
53634b254c5SRichard Tran Mills .vb
53734b254c5SRichard Tran Mills       -sys1_regressor_method linear -sys1_regressor_regularizer_weight 1.2
53834b254c5SRichard Tran Mills       -sys2_regressor_method linear -sys2_regressor_regularizer_weight 1.1
53934b254c5SRichard Tran Mills .ve
54034b254c5SRichard Tran Mills 
54134b254c5SRichard Tran Mills .seealso: [](ch_regressor), `PetscRegressor`, `PetscRegressorSetFromOptions()`, `PetscRegressorAppendOptionsPrefix()`, `PetscRegressorGetOptionsPrefix()`
54234b254c5SRichard Tran Mills @*/
PetscRegressorSetOptionsPrefix(PetscRegressor regressor,const char p[])54334b254c5SRichard Tran Mills PetscErrorCode PetscRegressorSetOptionsPrefix(PetscRegressor regressor, const char p[])
54434b254c5SRichard Tran Mills {
54534b254c5SRichard Tran Mills   PetscFunctionBegin;
54634b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
54734b254c5SRichard Tran Mills   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)regressor, p));
54834b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
54934b254c5SRichard Tran Mills }
55034b254c5SRichard Tran Mills 
55134b254c5SRichard Tran Mills /*@
55234b254c5SRichard Tran Mills   PetscRegressorAppendOptionsPrefix - Appends to the prefix used for searching for all PetscRegressor options in the database.
55334b254c5SRichard Tran Mills 
55434b254c5SRichard Tran Mills   Logically Collective
55534b254c5SRichard Tran Mills 
55634b254c5SRichard Tran Mills   Input Parameters:
55734b254c5SRichard Tran Mills + regressor - the `PetscRegressor` solver context
55834b254c5SRichard Tran Mills - p         - the prefix string to prepend to all `PetscRegressor` option requests
55934b254c5SRichard Tran Mills 
56034b254c5SRichard Tran Mills   Level: advanced
56134b254c5SRichard Tran Mills 
56234b254c5SRichard Tran Mills   Note:
56334b254c5SRichard Tran Mills   A hyphen (-) must NOT be given at the beginning of the prefix name.
56434b254c5SRichard Tran Mills   The first character of all runtime options is automatically the hyphen.
56534b254c5SRichard Tran Mills 
56634b254c5SRichard Tran Mills .seealso: [](ch_regressor), `PetscRegressor`, `PetscRegressorSetFromOptions()`, `PetscRegressorSetOptionsPrefix()`, `PetscRegressorGetOptionsPrefix()`
56734b254c5SRichard Tran Mills @*/
PetscRegressorAppendOptionsPrefix(PetscRegressor regressor,const char p[])56834b254c5SRichard Tran Mills PetscErrorCode PetscRegressorAppendOptionsPrefix(PetscRegressor regressor, const char p[])
56934b254c5SRichard Tran Mills {
57034b254c5SRichard Tran Mills   PetscFunctionBegin;
57134b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
57234b254c5SRichard Tran Mills   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)regressor, p));
57334b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
57434b254c5SRichard Tran Mills }
57534b254c5SRichard Tran Mills 
57634b254c5SRichard Tran Mills /*@
57734b254c5SRichard Tran Mills   PetscRegressorGetOptionsPrefix - Gets the prefix used for searching for all
57834b254c5SRichard Tran Mills   PetscRegressor options in the database
57934b254c5SRichard Tran Mills 
58034b254c5SRichard Tran Mills   Not Collective
58134b254c5SRichard Tran Mills 
58234b254c5SRichard Tran Mills   Input Parameter:
58334b254c5SRichard Tran Mills . regressor - the `PetscRegressor` context
58434b254c5SRichard Tran Mills 
58534b254c5SRichard Tran Mills   Output Parameter:
58634b254c5SRichard Tran Mills . p - pointer to the prefix string used is returned
58734b254c5SRichard Tran Mills 
58834b254c5SRichard Tran Mills   Fortran Notes:
58934b254c5SRichard Tran Mills   Pass in a string 'prefix' of sufficient length to hold the prefix.
59034b254c5SRichard Tran Mills 
59134b254c5SRichard Tran Mills   Level: advanced
59234b254c5SRichard Tran Mills 
59334b254c5SRichard Tran Mills .seealso: [](ch_regressor), `PetscRegressor`, `PetscRegressorSetFromOptions()`, `PetscRegressorSetOptionsPrefix()`, `PetscRegressorAppendOptionsPrefix()`
59434b254c5SRichard Tran Mills @*/
PetscRegressorGetOptionsPrefix(PetscRegressor regressor,const char * p[])59534b254c5SRichard Tran Mills PetscErrorCode PetscRegressorGetOptionsPrefix(PetscRegressor regressor, const char *p[])
59634b254c5SRichard Tran Mills {
59734b254c5SRichard Tran Mills   PetscFunctionBegin;
59834b254c5SRichard Tran Mills   PetscValidHeaderSpecific(regressor, PETSCREGRESSOR_CLASSID, 1);
59934b254c5SRichard Tran Mills   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)regressor, p));
60034b254c5SRichard Tran Mills   PetscFunctionReturn(PETSC_SUCCESS);
60134b254c5SRichard Tran Mills }
602