View | Details | Raw Unified | Return to bug 55079
Collapse All | Expand All

(-)poi/src/java/org/apache/poi/ss/formula/functions/Imaginary.java (+109 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
18
package org.apache.poi.ss.formula.functions;
19
20
import org.apache.poi.ss.formula.OperationEvaluationContext;
21
import org.apache.poi.ss.formula.eval.*;
22
23
import java.util.regex.Matcher;
24
import java.util.regex.Pattern;
25
26
/**
27
 * Implementation for Excel IMAGINARY() function.<p/>
28
 * <p/>
29
 * <b>Syntax</b>:<br/> <b>IMAGINARY  </b>(<b>Inumber</b>)<br/>
30
 * <p/>
31
 * Returns the imaginary coefficient of a complex number in x + yi or x + yj text format.
32
 * <p/>
33
 * Inumber     is a complex number for which you want the imaginary coefficient.
34
 * <p/>
35
 * Remarks
36
 * <ul>
37
 * <li>Use COMPLEX to convert real and imaginary coefficients into a complex number.</li>
38
 * </ul>
39
 *
40
 * @author cedric dot walter @ gmail dot com
41
 */
42
public class Imaginary extends Fixed1ArgFunction implements FreeRefFunction {
43
44
    public static final FreeRefFunction instance = new Imaginary();
45
46
    public static final String GROUP1_REAL_SIGN_REGEX = "([+-]?)";
47
    public static final String GROUP2_REAL_INTEGER_OR_DOUBLE_REGEX = "([0-9]+\\.[0-9]+|[0-9]*)";
48
    public static final String GROUP3_IMAGINARY_SIGN_REGEX = "([+-]?)";
49
    public static final String GROUP4_IMAGINARY_INTEGER_OR_DOUBLE_REGEX = "([0-9]+\\.[0-9]+|[0-9]*)";
50
    public static final String GROUP5_IMAGINARY_GROUP_REGEX = "([ij]?)";
51
52
    public static final Pattern COMPLEX_NUMBER_PATTERN
53
            = Pattern.compile(GROUP1_REAL_SIGN_REGEX + GROUP2_REAL_INTEGER_OR_DOUBLE_REGEX +
54
            GROUP3_IMAGINARY_SIGN_REGEX + GROUP4_IMAGINARY_INTEGER_OR_DOUBLE_REGEX + GROUP5_IMAGINARY_GROUP_REGEX);
55
56
    public static final int GROUP1_REAL_SIGN = 1;
57
    public static final int GROUP2_IMAGINARY_INTEGER_OR_DOUBLE = 2;
58
    public static final int GROUP3_IMAGINARY_SIGN = 3;
59
    public static final int GROUP4_IMAGINARY_INTEGER_OR_DOUBLE = 4;
60
61
    public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval inumberVE) {
62
        ValueEval veText1;
63
        try {
64
            veText1 = OperandResolver.getSingleValue(inumberVE, srcRowIndex, srcColumnIndex);
65
        } catch (EvaluationException e) {
66
            return e.getErrorEval();
67
        }
68
        String iNumber = OperandResolver.coerceValueToString(veText1);
69
70
        Matcher m = COMPLEX_NUMBER_PATTERN.matcher(iNumber);
71
        boolean result = m.matches();
72
73
        String imaginary = "";
74
        if (result == true) {
75
            String imaginaryGroup = m.group(5);
76
            boolean hasImaginaryPart = imaginaryGroup.equals("i") || imaginaryGroup.equals("j");
77
78
            if (imaginaryGroup.length() == 0) {
79
                return new StringEval(String.valueOf(0));
80
            }
81
82
            if (hasImaginaryPart) {
83
                String sign = "";
84
                String imaginarySign = m.group(GROUP3_IMAGINARY_SIGN);
85
                if (imaginarySign.length() != 0 && !(imaginarySign.equals("+"))) {
86
                    sign = imaginarySign;
87
                }
88
89
                String groupImaginaryNumber = m.group(GROUP4_IMAGINARY_INTEGER_OR_DOUBLE);
90
                if (groupImaginaryNumber.length() != 0) {
91
                    imaginary = sign + groupImaginaryNumber;
92
                } else {
93
                    imaginary = sign + "1";
94
                }
95
            }
96
        } else {
97
            return ErrorEval.NUM_ERROR;
98
        }
99
100
        return new StringEval(imaginary);
101
    }
102
103
    public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
104
        if (args.length != 1) {
105
            return ErrorEval.VALUE_INVALID;
106
        }
107
        return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0]);
108
    }
109
}
(-)poi/src/testcases/org/apache/poi/ss/formula/functions/TestImaginaryFunctionsFromSpreadsheet.java (+30 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
18
package org.apache.poi.ss.formula.functions;
19
20
/**
21
 * Tests Imaginary() as loaded from a test data spreadsheet.<p/>
22
 *
23
 * @author cedric dot walter @ gmail dot com
24
 */
25
public class TestImaginaryFunctionsFromSpreadsheet extends BaseTestFunctionsFromSpreadsheet {
26
27
    protected String getFilename() {
28
        return "ImaginaryFunctionTestCaseData.xls";
29
    }
30
}
(-)poi/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java (-1 / +1 lines)
Lines 112-118 Link Here
112
        r(m, "HEX2OCT", null);
112
        r(m, "HEX2OCT", null);
113
        r(m, "IFERROR", IfError.instance);
113
        r(m, "IFERROR", IfError.instance);
114
        r(m, "IMABS", null);
114
        r(m, "IMABS", null);
115
        r(m, "IMAGINARY", null);
115
        r(m, "IMAGINARY", Imaginary.instance);
116
        r(m, "IMARGUMENT", null);
116
        r(m, "IMARGUMENT", null);
117
        r(m, "IMCONJUGATE", null);
117
        r(m, "IMCONJUGATE", null);
118
        r(m, "IMCOS", null);
118
        r(m, "IMCOS", null);

Return to bug 55079