java - 使用 Java 将分段函数转换为 CSV 文件

标签 java csv mathml piecewise

我正在尝试创建一个 Java 函数,它将包含分段函数的字符串转换为可用于绘图的 CSV 文件。例如这个表达式:

if (time < 60) then (0.1) else ( if (time > 66.0115) then (0.1) else 1)

将转换为:

File pwl.csv
time , output
0 , 0.1 
59.9, 0.1
60, 1
66.0115, 1
66.11149999999999, 0.1
132.023, 0.1
End File

转换器需要能够处理各种分段函数,包括:

if (t > 0.5) then (2) else 3
if (t >= 0.5) then (2) else 3
if (t < 0.5) then (2) else 3
if (t <= 0.5) then (2) else 3
if ((t >= 3600) & (t <= 3660)) then (25) else 0

我已经能够编写转换第一个示例的代码,但它实际上只适用于该特定函数,并且我正在寻找更通用的解决方案。对这个问题有什么想法吗?

这些分段函数最初来自 MathML 文件,因此也欢迎任何有关从 MathML 直接转换为 CSV 的建议。

最佳答案

这似乎有效 - 有点。无论如何,这可能是一个好的开始。

public class CSVFun {
  // Where to start the scan of the function.
  static final double Start = 0.0;
  // End of scan.
  static final double End = 10000.0;
  // Fine enough to detect a change in the function.
  static final double BigStep = 0.1;
  // Finest resolution.
  static final double SmallStep = 0.000000001;

  // Work out some csv for a function.
  private static void csv(F f) {
    System.out.println("Function: " + f);
    // Start at 0.
    double t = Start;
    double ft = f.f(t);
    System.out.println(t + "," + ft);
    while (t < End) {
      // Walk to the end.
      double step = BigStep;
      // Find a break point.
      while (t < End && f.f(t) == ft) {
        t += step;
      }
      if (t < End) {
        // Back one.
        t -= step;
        // Zoom in on the transition point.
        while (step > SmallStep) {
          // Go smaller.
          step /= 10;
          // Walk forward.
          while (t < End && f.f(t) == ft) {
            t += step;
          }
          // Back one.
          t -= step;
        }
        // Before
        System.out.println(t + "," + ft);
        // One more forward.
        t += step;
      }
      // Print.
      if (f.f(t) != ft) {
        ft = f.f(t);
        System.out.println(t + "," + ft);
      }
    }
  }

  // Tests the process with the sample functions below.
  public static void main(String[] args) {
    try {
      for (F f : F.values()) {
        csv(f);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  // The sample functions - Encoded in Java
  enum F {
    A {
      @Override
      double f(double t) {
        if (t < 60) {
          return (0.1);
        }
        if (t > 66.0115) {
          return (0.1);
        }
        return 1;
      }
    },
    B {
      @Override
      double f(double t) {
        if (t > 0.5) {
          return 2;
        }
        return 3;
      }
    },
    C {
      @Override
      double f(double t) {
        if (t >= 0.5) {
          return 2;
        }
        return 3;
      }
    },
    D {
      @Override
      double f(double t) {
        if (t < 0.5) {
          return 2;
        }
        return 3;
      }
    },
    E {
      @Override
      double f(double t) {
        if (t <= 0.5) {
          return 2;
        }
        return 3;
      }
    },
    F {
      @Override
      double f(double t) {
        if ((t >= 3600) & (t <= 3660)) {
          return 25;
        }
        return 0;
      }
    },;

    abstract double f(double t);
  }
}

输出:

Function: A
0.0,0.1
59.999999999000565,0.1
60.00000000000056,1.0
66.01149999900045,1.0
66.01150000000045,0.1
Function: B
0.0,3.0
0.49999999999999994,3.0
0.500000001,2.0
Function: C
0.0,3.0
0.49999999999999983,3.0
0.5000000009999999,2.0
Function: D
0.0,2.0
0.49999999999999983,2.0
0.5000000009999999,3.0
Function: E
0.0,2.0
0.49999999999999994,2.0
0.500000001,3.0
Function: F
0.0,0.0
3599.9999999998213,0.0
3600.0000000008213,25.0
3659.999999999771,25.0
3660.000000000771,0.0

我认为这很接近。

关于java - 使用 Java 将分段函数转换为 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11216294/

相关文章:

java - 如何更新自定义适配器中的 ListView ?

python - 使用python从CSV插入到oracle表中

csv - flask pythonanywhere服务器中的文件结构?

python - python-mathdom 的替代品

java - Spring Mvc 和 MediaType 用于在 @RequestMapping 中使用获取休息请求

java - 好的做法? REST 中的一两个对象模型用于创建和获取资源

csv - 从 csv 导入文件创建 Neo4j 关系

javascript - 如何将 Latex 转换为 mathml

java - 未能找到包含/storage/emulated/0/Android/data/的已配置根目录