2012-06-26 1 views
0

조각 별 함수가 포함 된 문자열을 그래프로 사용할 수있는 CSV 파일로 변환하는 Java 함수를 만들려고합니다. 예를 들어,이 식 :Java를 사용하여 조각 별 함수를 CSV 파일로 변환

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 

나는했습니다 :

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 (time < 60) then (0.1) else (if (time > 66.0115) then (0.1) else 1) 

는로 변환 할 것이다 첫 번째 예제를 변환 한 코드를 작성할 수 있지만 실제로는 특정 함수에서만 작동하며보다 일반적인 솔루션을 찾고 있습니다. 문제에 대한 어떤 생각?

이러한 조각 별 기능은 원래 MathML 파일에서 가져온 것이므로 MathML에서 CSV 로의 직접 변환에 대한 제안도 환영합니다.

+0

Java 클래스에서 사용할 수있는 '다양한 조각 별'기능이 있습니까? – sperumal

+0

MathML 인터프리터를 찾고 있다면 존재하지 않는다. http://stackoverflow.com/questions/2973248/is-there-a-library-to-evaluate-mathml-expressions를 참조하십시오. – sperumal

+0

자바에있을 필요가없는 경우 R을 확인해보십시오. 함수를 작성하고 값을 입력하여 결과를 그래프로 표시하십시오. – Jochen

답변

1

이것은 작동하는 것 같습니다. 어쨌든 좋은 시작일 것입니다.

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 

.

+0

이것은 확실히 시작이지만, 필자의 주요 문제는 좀 더 일반적인 변환기를 만들어서 각 piecewise 식에 특별한 경우를 쓸 필요가 없기 때문입니다. – user1026739

+0

나는 (A, B, C, D, E 및 F와 같이) 예제를 코드화하고 각각에 대한 csv를 생성하는 일반 알고리즘을 사용했습니다. 나는 당신의 질문에 대해 오해하지 않는 한 모든 반올림 규칙이 남아 있다고 생각합니다. – OldCurmudgeon

+0

아, 나는 네가 한 일을 오해했다. – user1026739

관련 문제