def test_trend(self): N = 100 M = 1000 P = 10 mean = ot.SymbolicFunction("x", "sign(x)") cov = ot.SquaredExponential([1.0], [0.1]) mesh = ot.IntervalMesher([N]).build(ot.Interval(-2.0, 2.0)) process = ot.GaussianProcess(ot.TrendTransform(mean, mesh), cov, mesh) sample = process.getSample(M) algo = ot.KarhunenLoeveSVDAlgorithm(sample, 1e-6) algo.run() result = algo.getResult() trend = ot.TrendTransform( ot.P1LagrangeEvaluation(sample.computeMean()), mesh) sample2 = process.getSample(P) sample2.setName('reduction of sign(x) w/o trend') reduced1 = ot.KarhunenLoeveReduction(result)(sample2) reduced2 = ot.KarhunenLoeveReduction(result, trend)(sample2) g = sample2.drawMarginal(0) g.setColors(["red"]) g1 = reduced1.drawMarginal(0) g1.setColors(["blue"]) drs = g1.getDrawables() for i, d in enumerate(drs): d.setLineStyle("dashed") drs[i] = d g1.setDrawables(drs) g.add(g1) g2 = reduced2.drawMarginal(0) g2.setColors(["green"]) drs = g2.getDrawables() for i, d in enumerate(drs): d.setLineStyle("dotted") drs[i] = d g2.setDrawables(drs) g.add(g2) if 0: from openturns.viewer import View View(g).save('reduction.png')
#! /usr/bin/env python import openturns as ot import openturns.testing as ott mesh = ot.RegularGrid(0.0, 1.0, 4) values = ot.Sample([[0.5], [1.5], [1.0], [-0.5]]) field = ot.Field(mesh, values) evaluation = ot.P1LagrangeEvaluation(field) x = [2.3] y = evaluation(x) print(y) ott.assert_almost_equal(y, [0.55]) # Learning sample on meshD mesher = ot.IntervalMesher([7, 7]) lowerbound = [-1.0, -1.0] upperBound = [1, 1] interval = ot.Interval(lowerbound, upperBound) meshD = mesher.build(interval) sample = ot.ProcessSample(meshD, 10, 1) field = ot.Field(meshD, 1) for k in range(sample.getSize()): field.setValues(ot.Normal().getSample(64)) sample[k] = field lagrange = ot.P1LagrangeEvaluation(sample) # New mesh mesh = ot.Mesh( ot.MonteCarloExperiment(ot.ComposedDistribution([ot.Uniform(-1, 1)] * 2), 200).generate())
import openturns as ot from openturns.viewer import View N = 100 M = 1000 P = 10 mean = ot.SymbolicFunction("x", "sign(x)") cov = ot.SquaredExponential([1.0], [0.1]) mesh = ot.IntervalMesher([N]).build(ot.Interval(-2.0, 2.0)) process = ot.GaussianProcess(ot.TrendTransform(mean, mesh), cov, mesh) sample = process.getSample(M) algo = ot.KarhunenLoeveSVDAlgorithm(sample, 1e-6) algo.run() result = algo.getResult() trend = ot.TrendTransform(ot.P1LagrangeEvaluation(sample.computeMean()), mesh) sample2 = process.getSample(P) sample2.setName('reduction of sign(x) w/o trend') reduced1 = ot.KarhunenLoeveReduction(result)(sample2) reduced2 = ot.KarhunenLoeveReduction(result, trend)(sample2) g = sample2.drawMarginal(0) g.setColors(["red"]) g1 = reduced1.drawMarginal(0) g1.setColors(["blue"]) drs = g1.getDrawables() for i, d in enumerate(drs): d.setLineStyle("dashed") drs[i] = d g1.setDrawables(drs) g.add(g1) g2 = reduced2.drawMarginal(0) g2.setColors(["green"])
dY1 = 0.5 * Y1 * (Y0 - 1.0) return [dY0, dY1] phi_func = ot.PythonFunction(2, 2, flow) # Create the mesh discretization = [N] * 2 mesh = ot.IntervalMesher(discretization).build(interval) # Covariance model covariance = ot.TensorizedCovarianceModel( [ot.SquaredExponential([0.2] * 2, [0.3])] * 2) process = ot.GaussianProcess(ot.TrendTransform(phi_func), covariance, mesh) field = process.getRealization() f = ot.Function(ot.P1LagrangeEvaluation(field)) ot.ResourceMap.SetAsUnsignedInteger("Field-LevelNumber", 64) ot.ResourceMap.SetAsScalar("Field-ArrowRatio", 0.01) ot.ResourceMap.SetAsScalar("Field-ArrowScaling", 0.03) graph = field.draw() print("f=", f.getInputDimension(), "->", f.getOutputDimension()) phi = ot.ValueFunction(f) solver = ot.RungeKutta(phi) initialState = [0.5, 1.0] timeGrid = ot.RegularGrid(0.0, 0.1, 10000) result = solver.solve(initialState, timeGrid) print(result) curve = ot.Curve(result) curve.setColor("red") curve.setLineWidth(2) graph.add(curve)
import openturns as ot from math import exp from matplotlib import pyplot as plt from openturns.viewer import View mesh = ot.RegularGrid(0.0, 1.0, 4) values = [[0.5], [1.5], [1.0], [-0.5]] field = ot.Field(mesh, values) func = ot.P1LagrangeEvaluation(field) func.setDescription(['$x$', '$y$']) graph = func.draw(-1.0, 4.0, 1024) cloud = ot.Cloud(mesh.getVertices(), values) cloud.setPointStyle("square") graph.add(cloud) graph.setColors(["blue", "red"]) fig = plt.figure(figsize=(10, 4)) plt.suptitle('P1 Lagrange interpolation') func_axis = fig.add_subplot(111) view = View(graph, figure=fig, axes=[func_axis], add_legend=False)