<commit_before>from SimPEG import *
import matplotlib.pyplot as plt


class LinearProblem(Problem.BaseProblem):
    """docstring for LinearProblem"""

    def __init__(self, model, G, **kwargs):
        Problem.BaseProblem.__init__(self, model, **kwargs)
        self.G = G

    def fields(self, m, u=None):
        return self.G.dot(m)

    def Jvec(self, m, v, u=None):
        return self.G.dot(v)

    def Jtvec(self, m, v, u=None):
        return self.G.T.dot(v)


def example(N):
    M = Mesh.TensorMesh([N])

    nk = 20
    jk = np.linspace(1.,20.,nk)
    p = -0.25
    q = 0.25

    g = lambda k: np.exp(p*jk[k]*M.vectorCCx)*np.cos(2*np.pi*q*jk[k]*M.vectorCCx)

    G = np.empty((nk, M.nC))

    for i in range(nk):
        G[i,:] = g(i)

    mtrue = np.zeros(M.nC)
    mtrue[M.vectorCCx > 0.3] = 1.
    mtrue[M.vectorCCx > 0.45] = -0.5
    mtrue[M.vectorCCx > 0.6] = 0



    model = Model.BaseModel(M)
    prob = LinearProblem(model, G)
    data = prob.createSyntheticSurvey(mtrue, std=0.01)

    return prob, data, model


if __name__ == '__main__':

    prob, data, model = example(100)
    M = prob.mesh

    reg = Regularization.Tikhonov(model)
    objFunc = ObjFunction.BaseObjFunction(data, reg)
    opt = Optimization.InexactGaussNewton(maxIter=20)
    inv = Inversion.BaseInversion(objFunc, opt)
    m0 = np.zeros_like(data.mtrue)

    mrec = inv.run(m0)

    plt.figure(1)
    for i in range(prob.G.shape[0]):
        plt.plot(prob.G[i,:])

    plt.figure(2)

    plt.plot(M.vectorCCx, data.mtrue, 'b-')
    plt.plot(M.vectorCCx, mrec, 'r-')
<commit_msg>Updates to linear problem<commit_after>from SimPEG import *
import matplotlib.pyplot as plt

class LinearSurvey(Survey.BaseSurvey):
    def projectFields(self, u):
        return u

class LinearProblem(Problem.BaseProblem):
    """docstring for LinearProblem"""

    surveyPair = LinearSurvey

    def __init__(self, model, G, **kwargs):
        Problem.BaseProblem.__init__(self, model, **kwargs)
        self.G = G

    def fields(self, m, u=None):
        return self.G.dot(m)

    def Jvec(self, m, v, u=None):
        return self.G.dot(v)

    def Jtvec(self, m, v, u=None):
        return self.G.T.dot(v)


def example(N):
    M = Mesh.TensorMesh([N])

    nk = 20
    jk = np.linspace(1.,20.,nk)
    p = -0.25
    q = 0.25

    g = lambda k: np.exp(p*jk[k]*M.vectorCCx)*np.cos(2*np.pi*q*jk[k]*M.vectorCCx)

    G = np.empty((nk, M.nC))

    for i in range(nk):
        G[i,:] = g(i)

    mtrue = np.zeros(M.nC)
    mtrue[M.vectorCCx > 0.3] = 1.
    mtrue[M.vectorCCx > 0.45] = -0.5
    mtrue[M.vectorCCx > 0.6] = 0



    model = Model.BaseModel(M)
    prob = LinearProblem(model, G)
    survey = prob.createSyntheticSurvey(mtrue, std=0.01)

    return prob, survey, model


if __name__ == '__main__':

    prob, survey, model = example(100)
    M = prob.mesh

    reg = Regularization.Tikhonov(model)
    objFunc = ObjFunction.BaseObjFunction(survey, reg)
    opt = Optimization.InexactGaussNewton(maxIter=20)
    inv = Inversion.BaseInversion(objFunc, opt)
    m0 = np.zeros_like(survey.mtrue)

    mrec = inv.run(m0)

    plt.figure(1)
    for i in range(prob.G.shape[0]):
        plt.plot(prob.G[i,:])

    plt.figure(2)

    plt.plot(M.vectorCCx, survey.mtrue, 'b-')
    plt.plot(M.vectorCCx, mrec, 'r-')

{'fraction_non_alphanumeric': 0.15587967183226983, 'fraction_numerical': 0.02187784867821331, 'mean_word_length': 2.665924276169265, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4289965', 'n_tokens_mistral': 1417, 'n_tokens_neox': 1308, 'n_words': 264}