# Coding Different Trigonometric Functions in Processing

**URL:** <https://discourse.processing.org/t/coding-different-trigonometric-functions-in-processing/22257>\
**Category:** Coding Questions\
**Created:** [June 29, 2020, 6:07pm UTC](https://discourse.processing.org/t/coding-different-trigonometric-functions-in-processing/22257 "2020-06-29T18:07:55Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)\
**Post date:** [June 29, 2020, 6:07pm UTC](https://discourse.processing.org/t/coding-different-trigonometric-functions-in-processing/22257/1 "2020-06-29T18:07:55Z")

</div>

I am trying to code functions that will put a variable or number through the sigmoid function and another one for the step function.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 29, 2020, 9:49pm UTC](https://discourse.processing.org/t/coding-different-trigonometric-functions-in-processing/22257/2 "2020-06-29T21:49:46Z")

</div>

Yes

Can you please show your entire code and tell us what the problem is?

Usually we don’t post solutions

---

<div class="post-metadata">

**Author:** ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)\
**Post date:** [June 29, 2020, 10:14pm UTC](https://discourse.processing.org/t/coding-different-trigonometric-functions-in-processing/22257/3 "2020-06-29T22:14:51Z")

</div>

I don’t know how to code it, so I am just looking for a general idea to start.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 30, 2020, 6:20am UTC](https://discourse.processing.org/t/coding-different-trigonometric-functions-in-processing/22257/4 "2020-06-30T06:20:59Z")

</div>

My first sketch here [Mathematic function](https://discourse.processing.org/t/mathematic-function/19535/13)

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [June 30, 2020, 7:23am UTC](https://discourse.processing.org/t/coding-different-trigonometric-functions-in-processing/22257/5 "2020-06-30T07:23:45Z")

</div>

Sigmoid is 1/(1 + e ^ x)

You need to use return lest you are saving the values directly. E should be exp() and power should be pow( input, power).

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 30, 2020, 10:58am UTC](https://discourse.processing.org/t/coding-different-trigonometric-functions-in-processing/22257/6 "2020-06-30T10:58:53Z")

</div>

> [@Adi](#):
>
> I don’t know how to code it, so I am just looking for a general idea to start.

One of the best tools in a programmers tool chest is knowing the resources available to you and learning to navigate and use them.

This is a very short list:

> **Resources \< Click here to expand !**
>
> I encourage you to review the resources available here:
> 
> - The Processing _website_ has references, examples, tutorials, etc.  
> [https://processing.org/](https://processing.org/)
> - The Processing PDE (IDE) has lots to offer!  
> An exploration of the tabs will reveal what is available; libraries, tools and local examples there.
> - The Coding Train  
> [https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1\_aw](https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1_aw)  
> [https://thecodingtrain.com/](https://thecodingtrain.com/)
> - Happy Coding  
> [Processing Tutorials - Happy Coding](https://happycoding.io/tutorials/processing/)
> - The source code can give insight if you are adventurous:  
> [GitHub - processing/processing: Source code for the Processing Core and Development Environment (PDE)](https://github.com/processing/processing)  
> Many of the Processing functions are here:  
> [processing/core/src/processing/core/PApplet.java at master · processing/processing · GitHub](https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java)
> - The Processing Foundation  
> [https://processingfoundation.org/](https://processingfoundation.org/)  
> Check out the tabs.  
> In the education tab, there is a reference to the Coding Train.
> - And the Internet. `:)`

`:)`

---

<div class="post-metadata">

**Author:** ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)\
**Post date:** [June 30, 2020, 5:49pm UTC](https://discourse.processing.org/t/coding-different-trigonometric-functions-in-processing/22257/7 "2020-06-30T17:49:16Z")

</div>

Thanks for your replies, I did search the internet, but I never really found a way to put variables in it, I probably didn’t look hard enough. @paulgoux should I do this?:

```auto
float e = exp(1.0)
1/(1+pow(e, x);

```

I don’t know what x should be, is it the input?  
I want the sigmoid to go from 0 to 1, because it would find the probability, so I thought that the 1 in (1+e^x) would be 0, but I don’t know if I am right.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [June 30, 2020, 6:33pm UTC](https://discourse.processing.org/t/coding-different-trigonometric-functions-in-processing/22257/8 "2020-06-30T18:33:11Z")

</div>

X is the variable you are testing the sigmoid should return a value based on that calculation

---

<div class="post-metadata">

**Author:** ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)\
**Post date:** [June 30, 2020, 7:02pm UTC](https://discourse.processing.org/t/coding-different-trigonometric-functions-in-processing/22257/9 "2020-06-30T19:02:45Z")

</div>

Ok so x is the input. One more request @paulgoux, I would like the same function just the other way around, in your equation negative numbers get closer to 1, and positive numbers get closer to 0. Is there a way to make negative numbers closer to 0, and positive numbers closer to 1.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [June 30, 2020, 7:08pm UTC](https://discourse.processing.org/t/coding-different-trigonometric-functions-in-processing/22257/10 "2020-06-30T19:08:01Z")

</div>

Just do 1 - your sigmoid function

---

<div class="post-metadata">

**Author:** ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)\
**Post date:** [June 30, 2020, 7:26pm UTC](https://discourse.processing.org/t/coding-different-trigonometric-functions-in-processing/22257/12 "2020-06-30T19:26:46Z")

</div>

Never mind, I got it, Thanks for your help everyone 😃.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 2, 2020, 9:01pm UTC](https://discourse.processing.org/t/coding-different-trigonometric-functions-in-processing/22257/13 "2020-07-02T21:01:11Z")

</div>

Hello,

can you please post your solution/ entire sketch?

---

<div class="post-metadata">

**Author:** ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)\
**Post date:** [July 4, 2020, 11:07pm UTC](https://discourse.processing.org/t/coding-different-trigonometric-functions-in-processing/22257/14 "2020-07-04T23:07:18Z")

</div>

Hi, sorry for the late reply, here it is.

```auto
1/(1+pow(exp(1), x);//Logistic Sig. X is the input.
((pow(exp(1), x))-(pow(exp(1),(x*-1))))-((pow(exp(1), x))+(pow(exp(1),(x*-1))))// Tanh X is the input.

```

There is probably a better way for the Tanh.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [July 4, 2020, 11:50pm UTC](https://discourse.processing.org/t/coding-different-trigonometric-functions-in-processing/22257/15 "2020-07-04T23:50:05Z")

</div>

glad you figured it out.

might want to take a look at this example to compare your implementation. In this example sigmoid is precalculated at setup and stored in an array to be read later, saving compute time later. You don’t need to read all of it, theres a link to his github for downloads, you’ll want to download part4

> **[Let’s code a Neural Network from scratch — Part 1](https://medium.com/typeme/lets-code-a-neural-network-from-scratch-part-1-24f0a30d7d62)**
>
> Part 1, Part 2 & Part 3

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 5, 2020, 7:17am UTC](https://discourse.processing.org/t/coding-different-trigonometric-functions-in-processing/22257/16 "2020-07-05T07:17:28Z")

</div>

Great! I also heard that pow is really slow

You can write a function where the multiplying is done in a for loop x times without pow

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 5, 2020, 3:09pm UTC](https://discourse.processing.org/t/coding-different-trigonometric-functions-in-processing/22257/17 "2020-07-05T15:09:53Z")

</div>

> [@Chrisir](#):
>
> I also heard that pow is really slow
> 
> You can write a function where the multiplying is done in a for loop x times without pow

You can write a function for **integer exponents** and where execution time is important to speed things up.  
Discussed and tested here:

> [@How to cube a sine/cosine function in processing](https://discourse.processing.org/t/how-to-cube-a-sine-cosine-function-in-processing/17257/10):
>
> Hello, Back to your original question… This was the fastest (667) execution in my tests for your original request: float angle = random(0, TAU); float tmp = cos(angle); float cosCubed = tmp\*tmp\*tmp; Compared to (1127): float angle = random(0, TAU); float cosCubed = cos(angle)\*cos(angle)\*cos(angle); Slowest (1275) : float angle = random(0, TAU); float cosCubed = pow(cos(angle), 3); The numbers (ms) were the outcome of a stress test using modified code from @jeremydouglass in this to…

In the example provided he did not require pow():

```auto
float x;
float y;

void setup()
  {
  size(500, 500);
  strokeWeight(2);
  translate(width/2, height/2 - 10);

//Using integer steps in formula used  
for (int i = -10; i<10; i+= 1)
    {
    y = 200/(1+pow(exp(1), i));
    point(10*i, 1-y);
    }

  translate(0, height/2);    
    
// Simplified and using float steps of 0.5 
  for (float i = -10; i<10; i+= .5)
    {
    y = 200/(1+exp(i));
    point(10*i, 1-y);
    }
  }

```

References:

> **[exp() / Reference](https://processing.org/reference/exp_.html)**
>
> Returns Euler's number e (2.71828...) raised to the power of the value parameter.

> **[pow() / Reference](https://processing.org/reference/pow_.html)**
>
> Facilitates exponential expressions. The pow() function is an efficient way of multiplying numbers by themselves (or their reciprocal) in large quantities. For example, pow(3, 5) is equi…

[Processing source code for exp() and pow()](https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L4513)

`:)`

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/3c4efcb083821d404c74959b166537f71deb38d9.png)

---

<div class="post-metadata">

**Author:** ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)\
**Post date:** [July 6, 2020, 2:45am UTC](https://discourse.processing.org/t/coding-different-trigonometric-functions-in-processing/22257/18 "2020-07-06T02:45:28Z")

</div>

Thanks, this looks like a really interesting article, just what I was looking for!
