# Write a Sine-Deriving Machine

### The Challenge

Your challenge is to write a program that evaluates the following function:

f(x, t) = d^x / dt^x (sin t)

That is, the x-th derivative of sin t. In case you aren't familiar with calculus, here is a table of derivatives for the sine function:

...

f(-1, t) = -cos t

f(0, t) = sin t

f(1, t) = cos t

f(2, t)= -sin t

...

The sequence repeats these 4 functions.

### The Inputs

Your program should take in an integer to be used as the x value in the function, and a float to be used at the t value in the function.

### The Output

Your program should output the value of f(x, t) given the integer x and float t.

### Scoring

This is , so the shortest answer (in bytes) wins.

• Are you sure d^-1 / dt^-1 (sin t) = -cos t ? Commented Aug 13, 2016 at 18:52
• What precision should this be to? Commented Aug 13, 2016 at 18:58
• Test cases would be great.
– lynn
Commented Aug 13, 2016 at 22:26
• Okay so trigonometry builtins seem to be allowed. Just a comment, who is not familiar with calculus does not understand the table either :P Commented Aug 14, 2016 at 0:46
• Today, I am more in the mood of writing an exponential function e^x deriving machine. Commented May 26, 2017 at 21:37

## Python, 42 bytes

from math import*
lambda x,n:sin(x+pi/2*n)


Uses the fact that differentiating shifts the function by pi/2 left.

• I'm such an idiot Commented Aug 13, 2016 at 19:12

## Actually, 5 bytes

╦*½+S


Try it online

Takes input as t\nx.

Explanation:

╦*½+S
╦*     pi*n
½    half that
S  sine


# C (gcc), 30 bytes

#define f(x,n)sin(x+n*acos(0))


x%n=sin\$x+pi/2*n


Defines a binary operator %.

λ 2.0 % (-3)
-0.4161468365471426


Uses the fact that differentiating shifts the function by pi/2 left.

Making it point-free was longer (takes n x):

(sin.).(+).(pi/2*)


# Convex (IDE version only), 6 bytes

P½*+ms


Try it online!

After futher investigation, with the help of Dennis in chat, it was found that the command-line version of Convex (what TIO uses) has a bug that breaks the code (the link above contains a workaround). However, the IDE version of Convex, avaliable on GitHub before the challenge was posted, does not have this bug. Here is a link to the IDE version that was avaliable before the challenge was posted: Convex IDE.

# Mathematica 14 bytes

With help from Martin Ender,

Sin[#2+Pi/2#]&

• Shouldn't x be #2? Commented Aug 13, 2016 at 19:54
• At the same time, you don't need the *, so you'll end up with the same byte count. Commented Aug 13, 2016 at 21:14

# J, 17 bytes

4 :'1&o.d.(4|x)y'


Computes the (x%4)th derivative of sine, and then apply to right argument.

## Usage:

>> f =: 4 :'1&o.d.(4|x)y'
>> _1 0 1 2 3 (f"0 0) 0
<< _1 0 1 0 _1


where >> is STDIN and << is STDOUT.

• Why do you need to compute the (x%4)? Wouldn't it be simpler to just use the xth derivative? (Not a J user so this might be a stupid question) Commented Aug 13, 2016 at 22:45
• @StevenH. Because stupid question requires negative numbers. Commented Aug 14, 2016 at 2:58

# Julia, 17 bytes

x\t=x*π/2+t|>sin


Try it online!

## R, 26 bytes

function(t,x)sin(t+pi/2*x)


Exactly what it says on the tin...

# C (gcc), 5346 45 bytes

#import<math.h>
#define f(x,n)sin(x+M_PI/2*n)


Probably Ideone doesn't have glibc, says M_PI is undeclared.

• M_PI is not in math.h: stackoverflow.com/questions/5007925/….
– orlp
Commented Aug 13, 2016 at 23:00
• But...it's there on glibc? sourceware.org/git/?p=glibc.git;a=blob;f=math/… Commented Aug 13, 2016 at 23:19
• Not in "C", the language you specified in the top of your answer. C as implemented by gcc has it, so you can write "C (gcc)", but C is a language defined by an abstract standard, and makes no mention of M_PI.
– orlp
Commented Aug 13, 2016 at 23:24

# JavaScript (ES6), 29 bytes

x=>t=>Math.sin(t+Math.PI/2*x)


Usage:

f=x=>t=>Math.sin(t+Math.PI/2*x)
f(x)(t)


## Lua 5.x, 37 bytes

x,n=...return math.sin(x+math.pi/2*n)


usage:

> print(loadfile('sine.lua')(1,0))
0.8414709848079
1.2246467991474e-16
-1.0
-2.4492935982947e-16
1.0


## Matlab, 19 bytes

@(x,t)sin(t+x*pi/2)

• So why the downvote? Commented Aug 14, 2016 at 23:59

# SQF, 29 bytes

Using the file-as-a-function format:

params["x","y"];sin(x+pi/2*y)


Call as [x, t] call NAME_OF_COMPILED_FUNCTION

# SmileBASIC, 25 bytes

INPUT X,N?SIN(X+N*PI()/2)


Probably shorter in some BASIC dialect that has a constant named PI rather than PI(). (Yes, PI() is not actually a function, those parentheses are a lie.)

# 05AB1E, 7 bytes

žq*;+Å½


žq        # Push PI
*       # Multiply it with the first (implicit) input t
+     # Add the second (implicit) input x