As of December 1, 2020, Focal Point is retired and repurposed as a reference repository. We value the wealth of knowledge that's been shared here over the years. You'll continue to have access to this treasure trove of knowledge, for search purposes only.
Join the TIBCO Community TIBCO Community is a collaborative space for users to share knowledge and support one another in making the best use of TIBCO products and services. There are several TIBCO WebFOCUS resources in the community.
From the Home page, select Predict: WebFOCUS to view articles, questions, and trending articles.
Select Products from the top navigation bar, scroll, and then select the TIBCO WebFOCUS product page to view product overview, articles, and discussions.
Request access to the private WebFOCUS User Group (login required) to network with fellow members.
Former myibi community members should have received an email on 8/3/22 to activate their user accounts to join the community. Check your Spam folder for the email. Please get in touch with us at community@tibco.com for further assistance. Reference the community FAQ to learn more about the community.
I need to perform some calculations in my WebFOCUS report that involves using some trigonometric functions...cosine, tangent, and sine. Has anyone used cosine, tangent, or sine functions in their calculations?This message has been edited. Last edited by: FP Mod Chuck,
WF 8.1.05 Windows SQL Server 2014 HTML, PDF, EXCEL
What's the goal of your trigonometry? Are you operating on map coordinates perhaps? In that case it probably pays off to look at a GIS package for your calculations. I don't know how well WF supports those though.
If not, perhaps your easiest solution is to do your calculations in SQL, directly on the RDBMS where your data lives.
Be aware though that calls to custom WF functions can (probably) not be translated to SQL, so use those either in COMPUTEs (as opposed to DEFINEs) or on local tables (FOCUS, binary, CSV, etc).
WebFOCUS 8.1.03, Windows 7-64/2008-64, IBM DB2/400, Oracle 11g & RDB, MS SQL-Server 2005, SAP, PostgreSQL 11, Output: HTML, PDF, Excel 2010 : Member of User Group Benelux :
Thanks for the response Wep5622. The goal is strictly computing a ratio using cosine, tangent, or sine. For example, here is one of the calculations (Avg Power Factor) that I need to compute:
PF avg = cos(tan-1(kVArh/kWh))
I need to know how to reference the cosine and tangent within the function/subroutine.
WF 8.1.05 Windows SQL Server 2014 HTML, PDF, EXCEL
I have spent most of my 37 years in (Web)FOCUS using it as a language to track scientific projects. We worked a lot in spatial relations, so I have written a bunch of reports that use trig functions. The last couple of years I have been on the finance side and had to dig this out of my archives.
It is possible to create a C (or FORTRAN, etc) library containing those functions wrapped for use in FOCUS. The C programming language (and the others probably do to) has implementations for sin, cos, tan, atan, etc. that you can use in your functions.
Since you're working with pure numbers, you probably won't have to worry about memory allocation, so the problem boils down to writing some simple wrappers and getting them to compile as a library.
The resulting library should go in your WF server directory under wfs/user/. After that, you can call your functions just like that.
There are a few benefits to going this route over a DEFINE FUNCTION implementation, namely that it will probably perform better and that you're using an implementation that's well-established.
For a start of a C library, your exported functions would look something like this:
Where the first parameter of each function is the input value and the second is the result value.
You would use them like this:
TABLE FILE foo
PRINT
COMPUTE avg/D12.6 = COS(ATAN(kVArh / kWh, 'D12.6'), avg);
BY etc
END
If you're not comfortable writing C code, there are people who do that for $$$. IBI comes to mind, but perhaps a nearby computer science student or so can do that cheap (and hopefully reliable).
WebFOCUS 8.1.03, Windows 7-64/2008-64, IBM DB2/400, Oracle 11g & RDB, MS SQL-Server 2005, SAP, PostgreSQL 11, Output: HTML, PDF, Excel 2010 : Member of User Group Benelux :
Michael L Meagher - I ran this simple test below calling your F_TAN trig functions and I got data returned successfully. I used 3 as a input value. The output is below also.
TABLE FILE MDM_READINGS PRINT AMI_METER_ID COMPUTE FUNC_TEST/D12.8 = F_TAN(3); WHERE READLIMIT EQ 10; END -RUN
I need to compute the arc tangent (tan to the -1 power)of that corresponding calculation....and then the cosine of that, such as below. Do you have one that will calculate the arc tangent?
cos(tan-1(kVArh/kWh))
WF 8.1.05 Windows SQL Server 2014 HTML, PDF, EXCEL
If you searched the forum using "sine cosine" you would have found that this has been asked before. You will also find that someone asked about distance calculation as well.
T
In FOCUS since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2
WebFOCUS App Studio 8.2.06 standalone on Windows 10
Posts: 5694 | Location: United Kingdom | Registered: April 08, 2004
Tony - When I searched, I believe I searched the whole knowledge base and may have overlooked that post by you before. So, I see you posted functions to calculate the sine and cosine of an angle about a year ago and I see that Glenda found and posted a distance equation using ATAN2(y,x) in 2007. To clarify, I can use this ATAN2 equation where y is the numerator in my equation and the x is the denominator in my equation I posted above?
WF 8.1.05 Windows SQL Server 2014 HTML, PDF, EXCEL
The inverse tangent is a multivalued function and hence requires a branch cut in the complex plane, which the Wolfram Language's convention places at (-iinfty,-i] and [i,iinfty). This follows from the definition of tan^(-1)z as tan^(-1)z=1/2i[ln(1-iz)-ln(1+iz)].
WebFOCUS 8.1.03, Windows 7-64/2008-64, IBM DB2/400, Oracle 11g & RDB, MS SQL-Server 2005, SAP, PostgreSQL 11, Output: HTML, PDF, Excel 2010 : Member of User Group Benelux :
On a different note; where are your numbers coming from?
If that's an RDBMS, chances are that the RDBMS implemented arctan (and then the other trig functions as well).
Armed with that info, you could for example create a (database) view doing these calculations.
Then it's simply a matter of creating a master on that view and TABLEing away.
WebFOCUS 8.1.03, Windows 7-64/2008-64, IBM DB2/400, Oracle 11g & RDB, MS SQL-Server 2005, SAP, PostgreSQL 11, Output: HTML, PDF, Excel 2010 : Member of User Group Benelux :
Wep5622 - My kVArh and kWh numbers are coming from a Hive database. I can check around to see if there is an implementation of these functions for HIve. However, as I understand the Hive database will not support database view. Also, not sure if I can use SQL pass-thru to execute it though if there are implementation of these functions.
So do you know how this calculation applies that was posted in 2007:
It seems that an arctan function is something people want every few years, and so after some research, I wrote an atan defined function that should do the trick. This particular algorithm is accurate to about 14 digits, which should be adequate for any commercial application. *** BEWARE *** the routine has not been comprehensively tested so use it after your own testing and at your own risk.
DEFINE FUNCTION ATAN(XVAL/D12.2)
-* see pdf "A Guide to Approximations" Jack G. Ganssle 2001
PI/D20.18 = 3.141592653589793 ;
PIO6/D20.18 = .5235987755982989 ;
PIO12/D20.18 = .2617993877991494 ;
TANPI6/D20.18= .5773502691896258 ;
TANPI12/D20.18=.2679491924311227 ;
-* first, reduce the input to <= pi/12
SIGN/I1 = IF XVAL LT 0 THEN 1 ELSE 0 ;
XVAL = IF XVAL LT 0 THEN -XVAL ;
COMPL/I1 = IF XVAL GT 1.0 THEN 1 ELSE 0 ;
XVAL = IF XVAL GT 1.0 THEN 1.0/XVAL ;
REGION/I1 = IF XVAL GT TANPI12 THEN 1 ELSE 0;
XVAL = IF XVAL GT TANPI12 THEN
(XVAL-TANPI6)/(1.0+(TANPI6*XVAL)) ;
D1= 48.70107004404898;
D2= 49.53262637722543;
D3= 9.40604244231624;
D4= 48.701070044049962;
D5= 65.76631639089563;
D6= 21.587934067020262;
XSQ = XVAL * XVAL ;
-* now, compute the arc tangent
ATAN/D12.8 = (XVAL*(D1 +XSQ*(D2 + XSQ*D3))/(D4 + XSQ*(D5 + XSQ*(D6 + XSQ))));
-* finally, adjust for sign, region, and complement
ATAN = IF REGION THEN ATAN+PIO6 ;
ATAN = IF COMPL THEN PI/2.0 - ATAN ;
ATAN = IF SIGN THEN -ATAN ;
END
Originally posted by AMC: Wep5622 - My kVArh and kWh numbers are coming from a Hive database. I can check around to see if there is an implementation of these functions for HIve. However, as I understand the Hive database will not support database view. Also, not sure if I can use SQL pass-thru to execute it though if there are implementation of these functions.
Ah, we can scratch that option off the list then...
quote:
So do you know how this calculation applies that was posted in 2007:
That looks like a Taylor polynome. Those are approximations, but the more components you add, the more accurate they become.
WebFOCUS 8.1.03, Windows 7-64/2008-64, IBM DB2/400, Oracle 11g & RDB, MS SQL-Server 2005, SAP, PostgreSQL 11, Output: HTML, PDF, Excel 2010 : Member of User Group Benelux :
Trying to run a quick proof to set how accurate the functions are and I realised that the Sine and Cosine functions do not match what I use because you have to apply a fix for angles above 180 degrees.
To see what I mean, use the functions from Michael above add -RUN after the declaration of the functions and then add this code to chart the results from the functions for a full 360 degree spread.
You will see that the Sine and Tangent results are wrong.
EX -LINES * EDAPUT MASTER,TRIGDATA,CV,MEM
FILE=TRIGDATA,SUFFIX=FOC
SEGNAME=SEG1
FIELD=ANGLE, ,I3 ,I3 , $
EDAPUT*
-RUN
CREATE FILE TRIGDATA
MODIFY FILE TRIGDATA
FIXFORM ANGLE/A3
DATA
-REPEAT :Loop FOR &Angle FROM 1 TO 360;
&Angle
-:Loop
END
-RUN
GRAPH FILE TRIGDATA
PRINT COMPUTE SINE/D12.10 = F_SIN(ANGLE);
COMPUTE COSINE/D12.10 = F_COS(ANGLE);
COMPUTE TANGENT/D12.10 MISSING ON = IF F_TAN(ANGLE) GT 2 THEN MISSING
ELSE IF F_TAN(ANGLE) LT -2 THEN MISSING ELSE F_TAN(ANGLE);
BY ANGLE
ON GRAPH PCHOLD FORMAT JSCHART
ON GRAPH SET VZERO OFF
ON GRAPH SET AUTOFIT ON
ON GRAPH SET HTMLENCODE ON
ON GRAPH SET GRAPHDEFAULT OFF
ON GRAPH SET GRMERGE ADVANCED
ON GRAPH SET GRMULTIGRAPH 0
ON GRAPH SET GRLEGEND 0
ON GRAPH SET GRXAXIS 1
ON GRAPH SET LOOKGRAPH VLINE
ON GRAPH SET STYLE *
*GRAPH_SCRIPT
setPieDepth(0);
setPieTilt(0);
setDepthRadius(0);
setCurveFitEquationDisplay(false);
setPlace(true);
setUseSeriesShapes(false);
setMarkerSizeDefault(25);
setReportParsingErrors(false);
setSelectionEnableMove(false);
setTransparentBorderColor(getSeries(*), false);
setDisplay(getO1Title(),false);
setDisplay(getY1Title(),false);
setFillColor(getSeries(0),new Color(0,128,0));
setFillColor(getSeries(1),new Color(255,128,0));
setFillColor(getSeries(2),new Color(237,186,4));
setFillColor(getSeries(3),new Color(186,4,237));
setY1ScaleMin(-2);
setY1ScaleMax(2);
*END
ENDSTYLE
END
T
In FOCUS since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2
WebFOCUS App Studio 8.2.06 standalone on Windows 10
Posts: 5694 | Location: United Kingdom | Registered: April 08, 2004
I suppose an easy fix for F_SIN is to express it in terms of F_COS with a phase-shift:
DEFINE FUNCTION F_SIN(angle/D8.5)
F_SIN/D31.25 = F_COS(angle - 90);
END
Similarly, you can use the fact that TAN alpha = SIN alpha / COS alpha, where the TAN goes to +/- infinity when COS alpha = 0.
In fact, the posted code already does so, but it first re-defines the computation of F_COS and partially F_SIN.
Quite possibly there was a good reason to do so, but you can write it as simply:
DEFINE FUNCTION F_TAN(angle/D8.5)
COSIN/D31.25 = F_COS(angle);
SIN/D31.25 = F_SIN(angle);
F_TAN/D31.25 MISSING ON = IF COSIN EQ 0 THEN MISSING ELSE SIN/COSIN;
END
WebFOCUS 8.1.03, Windows 7-64/2008-64, IBM DB2/400, Oracle 11g & RDB, MS SQL-Server 2005, SAP, PostgreSQL 11, Output: HTML, PDF, Excel 2010 : Member of User Group Benelux :
To correct these errors is easy just modify the result depending upon the angle or use per Wep above.
Instead of repeating the code from the Cosine function in the Sine and Tangent functions you could just call the F_COS function. Of course, the F_COS function would need to be defined before the F_SIN or F_TAN.
DEFINE FUNCTION F_SIN(angle/D8.5)
F_SIN/D31.25 = F_COS(angle - 90) ;
END
DEFINE FUNCTION F_TAN(angle/D8.5)
COSIN/D31.25 = F_COS(angle) ;
SIN/D31.25 = F_SIN(angle) ;
F_TAN/D31.25 = SIN / COSIN;
END
I am having difficulties uploading an image to my webspace but, when I am able, I will post an image showing before and after.
T
In FOCUS since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2
WebFOCUS App Studio 8.2.06 standalone on Windows 10
Posts: 5694 | Location: United Kingdom | Registered: April 08, 2004
While looking into this, I was playing around with turning PI into a function so that it could easily be referenced, but it appears that FOCUS functions require at least one parameter and that makes the result a little ugly:
DEFINE FUNCTION PI(DUMMY/I1)
PI/D31.29 = 3.14159265358979;
END
TABLE FILE TRIGDATA
PRINT COMPUTE PI/D12.11 = PI(0);
IF READLIMIT EQ 1
END
I tried using LET PI = 3.14159265358979;, but that caused an error in the definition of F_COS: (FOC258) FIELDNAME OR COMPUTATIONAL ELEMENT NOT RECOGNIZED: PI
It would be nice if there were a DEFINE CONSTANT PI/D31.29 = 3.14159265358979 or something similar.
WebFOCUS 8.1.03, Windows 7-64/2008-64, IBM DB2/400, Oracle 11g & RDB, MS SQL-Server 2005, SAP, PostgreSQL 11, Output: HTML, PDF, Excel 2010 : Member of User Group Benelux :