|
| 1 | +--- |
| 2 | +title: series_cos |
| 3 | +description: 'This page explains how to use the series_cos function in APL.' |
| 4 | +--- |
| 5 | + |
| 6 | +The `series_cos` function returns the cosine of each element in a numeric array. You can use it to apply trigonometric transformations across entire time series or vectorized data in one step. This function is useful when you want to analyze periodic patterns, normalize angles, or apply mathematical transformations to series data such as request durations, response times, or trace latencies. |
| 7 | + |
| 8 | +You often use `series_cos` together with other series functions like `series_sin` and `series_tan` to perform mathematical modeling, anomaly detection, or seasonality analysis in logs and telemetry data. |
| 9 | + |
| 10 | +## For users of other query languages |
| 11 | + |
| 12 | +If you come from other query languages, this section explains how to adjust your existing queries to achieve the same results in APL. |
| 13 | + |
| 14 | +<AccordionGroup> |
| 15 | +<Accordion title="Splunk SPL users"> |
| 16 | + |
| 17 | +In Splunk SPL, trigonometric functions like `cos` operate on single field values, not on arrays. To compute cosine across multiple values, you typically expand the values into events and then apply the `eval cos(field)` transformation. In APL, `series_cos` works natively on dynamic arrays, so you can directly transform an entire series in one call. |
| 18 | + |
| 19 | +<CodeGroup> |
| 20 | +```sql Splunk example |
| 21 | +... | eval cos_val=cos(angle) |
| 22 | +```` |
| 23 | + |
| 24 | +```kusto APL equivalent |
| 25 | +print arr=dynamic([0, 1.57, 3.14]) |
| 26 | +| extend cos_arr=series_cos(arr) |
| 27 | +``` |
| 28 | + |
| 29 | +</CodeGroup> |
| 30 | + |
| 31 | +</Accordion> |
| 32 | +<Accordion title="ANSI SQL users"> |
| 33 | + |
| 34 | +ANSI SQL does not provide direct support for array-wide trigonometric functions. The `COS()` function only works on single numeric values. To achieve array-like functionality, you usually need to unnest arrays and apply `COS()` row by row. In APL, `series_cos` eliminates this need by directly accepting an array and returning a transformed array. |
| 35 | + |
| 36 | +<CodeGroup> |
| 37 | +```sql SQL example |
| 38 | +SELECT COS(angle) AS cos_val |
| 39 | +FROM Angles; |
| 40 | +``` |
| 41 | + |
| 42 | +```kusto APL equivalent |
| 43 | +print arr=dynamic([0, 1.57, 3.14]) |
| 44 | +| extend cos_arr=series_cos(arr) |
| 45 | +``` |
| 46 | + |
| 47 | +</CodeGroup> |
| 48 | + |
| 49 | +</Accordion> |
| 50 | +</AccordionGroup> |
| 51 | + |
| 52 | +## Usage |
| 53 | + |
| 54 | +### Syntax |
| 55 | + |
| 56 | +```kusto |
| 57 | +series_cos(array) |
| 58 | +``` |
| 59 | + |
| 60 | +### Parameters |
| 61 | + |
| 62 | +| Parameter | Type | Description | |
| 63 | +| --------- | -------------------------- | --------------------------- | |
| 64 | +| `array` | dynamic (array of numbers) | An array of numeric values. | |
| 65 | + |
| 66 | +### Returns |
| 67 | + |
| 68 | +A dynamic array where each element is the cosine of the corresponding input element. |
| 69 | + |
| 70 | +## Use case examples |
| 71 | + |
| 72 | +<Tabs> |
| 73 | +<Tab title="Log analysis"> |
| 74 | + |
| 75 | +You want to model periodic patterns in request durations by applying the cosine function to the values. This is useful if you want to normalize cyclical metrics for further analysis. |
| 76 | + |
| 77 | +**Query** |
| 78 | + |
| 79 | +```kusto |
| 80 | +['sample-http-logs'] |
| 81 | +| summarize durations=make_list(req_duration_ms) by id |
| 82 | +| extend cos_durations=series_cos(durations) |
| 83 | +``` |
| 84 | + |
| 85 | +[Run in Playground](https://play.axiom.co/axiom-play-qf1k/query?initForm=%7B%22apl%22%3A%22%5B'sample-http-logs'%5D%20%7C%20summarize%20durations%3Dmake_list(req_duration_ms)%20by%20id%20%7C%20extend%20cos_durations%3Dseries_cos(durations)%22%7D) |
| 86 | + |
| 87 | +**Output** |
| 88 | + |
| 89 | +| id | durations | cos_durations | |
| 90 | +| -- | ---------------- | ------------------------ | |
| 91 | +| u1 | [120, 300, 450] | [0.814, -0.990, -0.737] | |
| 92 | +| u2 | [50, 250, 400] | [0.965, -0.801, -0.966] | |
| 93 | + |
| 94 | +This query collects request durations per user ID and applies the cosine transformation to the entire array. |
| 95 | + |
| 96 | +</Tab> |
| 97 | +<Tab title="OpenTelemetry traces"> |
| 98 | + |
| 99 | +You want to apply trigonometric transformations to span durations to explore cyclical behavior in distributed traces. |
| 100 | + |
| 101 | +**Query** |
| 102 | + |
| 103 | +```kusto |
| 104 | +['otel-demo-traces'] |
| 105 | +| summarize spans=make_list(duration) by ['service.name'] |
| 106 | +| extend cos_spans=series_cos(spans) |
| 107 | +``` |
| 108 | + |
| 109 | +[Run in Playground](https://play.axiom.co/axiom-play-qf1k/query?initForm=%7B%22apl%22%3A%22%5B'otel-demo-traces'%5D%20%7C%20summarize%20spans%3Dmake_list(duration)%20by%20%5B'service.name'%5D%20%7C%20extend%20cos_spans%3Dseries_cos(spans)%22%7D) |
| 110 | + |
| 111 | +**Output** |
| 112 | + |
| 113 | +| service.name | spans | cos_spans | |
| 114 | +| --------------- | --------------------- | ----------------- | |
| 115 | +| frontend | [00:00:01, 00:00:03] | [0.540, -0.990] | |
| 116 | +| checkoutservice | [00:00:02, 00:00:04] | [-0.416, -0.653] | |
| 117 | + |
| 118 | +This query groups spans by service and computes the cosine for each span duration, which can be used in advanced mathematical modeling of latency patterns. |
| 119 | + |
| 120 | +</Tab> |
| 121 | +<Tab title="Security logs"> |
| 122 | + |
| 123 | +You want to explore whether cosine transformations reveal patterns in request durations for suspicious traffic sources. |
| 124 | + |
| 125 | +**Query** |
| 126 | + |
| 127 | +```kusto |
| 128 | +['sample-http-logs'] |
| 129 | +| summarize durations=make_list(req_duration_ms) by ['geo.country'] |
| 130 | +| extend cos_blocked=series_cos(durations) |
| 131 | +``` |
| 132 | + |
| 133 | +[Run in Playground](https://play.axiom.co/axiom-play-qf1k/query?initForm=%7B%22apl%22%3A%22%5B'sample-http-logs'%5D%20%7C%20summarize%20durations%3Dmake_list(req_duration_ms)%20by%20%5B'geo.country'%5D%20%7C%20extend%20cos_blocked%3Dseries_cos(durations)%22%7D) |
| 134 | + |
| 135 | +**Output** |
| 136 | + |
| 137 | +| geo.country | blocked_durations | cos_blocked | |
| 138 | +| ----------- | ------------------ | ------------------------ | |
| 139 | +| US | [200, 400, 600] | [-0.416, -0.653, 0.960] | |
| 140 | +| DE | [100, 250, 500] | [0.540, -0.801, 0.284] | |
| 141 | + |
| 142 | +This query applies the cosine function to blocked request durations grouped by country, which can help highlight periodic access attempts from malicious sources. |
| 143 | + |
| 144 | +</Tab> |
| 145 | +</Tabs> |
| 146 | + |
| 147 | +## List of related functions |
| 148 | + |
| 149 | +- [series_abs](/apl/scalar-functions/time-series/series-abs): Returns the absolute value of each element in an array. Use it to normalize negative values in arrays. |
| 150 | +- [series_acos](/apl/scalar-functions/time-series/series-acos): Computes the arccosine of each element in an array. Use when you want the inverse cosine. |
| 151 | +- [series_atan](/apl/scalar-functions/time-series/series-atan): Computes the arctangent of each element in an array. Use when you want the inverse tangent. |
| 152 | +- [series_sin](/apl/scalar-functions/time-series/series-sin): Returns the sine of each element in an array. Use it when analyzing cyclical data with a phase shift. |
| 153 | +- [series_tan](/apl/scalar-functions/time-series/series-tan): Returns the tangent of each element in an array. Use it when you want to transform arrays with tangent-based periodicity. |
0 commit comments