#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Oct  9 15:29:25 2025

@author: jean-mariefurbringer
"""



import numpy as np

def matmod(Mexp, model):
    """
    Computes the model matrix (design matrix) 
    from the matrix of experiments and 
    the model exponents.

    Parameters
    ----------
    Mexp : ndarray of shape (Nexp, Nfact)
        Matrix of experiments (each row = one experiment, 
        each column = one factor).
    model : ndarray of shape (Ncoef, Nfact)
        Matrix defining the exponents of each 
        term in the model.

    Returns
    -------
    X : ndarray of shape (Nexp, Ncoef)
        Model matrix, where each column corresponds 
        to one model term.

    """

    Ncoef, N = model.shape
    Nexp, Nfact = Mexp.shape

    if N != Nfact:
        raise ValueError("Incoherence in the nbr of factors")

    # Initialize model matrix
    X = np.ones((Nexp, Ncoef))

    # Compute each column of X
    for coef in range(Ncoef):
        colX = np.ones(Nexp)
        for fact in range(Nfact):
            colX *= Mexp[:, fact] ** model[coef, fact]
        X[:, coef] = colX

    return X

#-----------------------------------------------
# test of the function
#-----------------------------------------------

# define a matrix of experiments

E = np.array([
    [12, 1, 88, 3],
    [14, -1, 88, 3],
    [16, 1, 88, 3],
    [12, -1, 92, 3],
    [12, 0, 96, 3]
    
])

print(E)

model=np.array([
    [0,0,0,0],
    [1,0,0,0],
    [0,1,0,0],
    [0,1,1,0],
    [2, 0, 0, 0]
    
])

print(model)

model2=np.array([
    [0,0,0],
    [1,0,0],
    [0,1,0],
    [0,1,1],
    [2, 0, 0]
    
])

print(model)

# compute the model matrix
X=matmod(E,model)
print("model matrix")
print(X)


X2=matmod(E,model2)
print("model matrix")
print(X2)


