非常感谢你们对微积分的帮助。现在我需要一些帮助,我即将进行的三角学测试。
在测试中,我需要简化表达式。我将获得类似于4sin(x)/(2cos(x))的输入,并且必须生成一个更简单但等价的表达式(如2tan(x))。所有的角度都是在度,我们将假装没有除以零或无效域问题。(例如,我们假设tan(x+1)cot(x+1)sin(x)/sin(x)总是1)。
我偷偷溜进教授的办公室,得到了一份可能在考试中出现的100个问题的清单。请给我解决所有问题的密码。其中一些问题可能已经尽可能地简化了;如果是这样的话,请返回输入。
我需要简化表达式,但我也需要一个简短的程序(或函数),这样教授就不会注意到了。尝试最小化程序长度和所有解决方案的总长度之和。
为了澄清这一点,如果某些表达式的返回没有变化,或者甚至以一种等效但更长的形式返回,这是可以的。此外,程序只需要处理下面列出的表达式;它可以返回不正确的结果,甚至对其他输入中断。
如您所知,所有的问题都采用类似的格式。x是唯一使用的变量,没有空格,每个函数名后面都有括号。(函数是sin、cos、tan、sec、csc和tan。)没有嵌套函数,但函数内部可能有像3x+4.5或-2-.7x这样的表达式。指数可以使用,但只适用于函数(如sin(x-.5)^7),幂总是大于1的整数。乘法是通过串联表示的。
产出也必须采用这种格式。无效:sin x,sin(x)/cos(x)tan(x) 切线是乘的还是除法的?,cos(x 关闭所有括号,4*sin(x),sec(x)^1
我可以使用任何编程语言,除非它依赖于内置的简化或表达式处理功能。内置的三角功能是可以的。(在考试期间,我将有一个非常基本的科学计算器。)此外,由于我将阅读写在纸上的程序,我只能使用可打印的ASCII字符(0x20到0x7E)和换行符。
1/(sec(x))
4+sin(x)+3
(cos(7x+4)-sin(-3x))/(sin(-3x))
2sin(x)2tan(x).25cos(x)^3cos(x)+1+1
7cos(x)sec(x)/(14tan(x)csc(x))
sin(x)cos(x)/sec(x)
8sin(x)cos(x)sin(x)/tan(x)+8sin(x)
sin(x)^9cos(x)cot(x)sec(x)csc(x)sec(x)tan(x)/(cot(x)^2tan(x)cos(x)^4cot(x)sin(x))
tan(x)cos(x)csc(x)
tan(x+1)sin(x-1)cos(x+1)^3tan(x-1)^2sin(-x+1)csc(x+1)tan(-x+1)/(cos(x-1)cot(x+1))
(cos(2x)+cot(2x)-sin(2x)+csc(2x)^3)/(cot(2x)+sin(2x))
cos(90-x)cos(x)
sin(x+180)sec(450-x)
tan(-x)sin(x+90)sec(x-90)
tan(x+180)^2
cot(-x)cos(-x)
cot(180-x)^3cos(270-x)^2
sin(.1x-.2)sin(.1x-.2)sin(.1x-.2)sin(.2-.1x)sin(.2-.1x)
sin(x)
sin(90-x)sin(x)+cos(90-x)/sec(x)
tan(3x+2)cos(3x+2)/sin(3x+2)-1
cos(x)cos(x)cos(x)cos(x)cos(x)
sec(2x+1)sec(-1-2x)+sec(-2x-1)sec(2x+1)
cos(4x)cot(4x)tan(4x)sin(4x)csc(4x)
-cos(x)+cos(x)+sin(2x-4)-1/csc(2x-4)
sec(x)sec(x+2)cot(x)tan(x-2)tan(x+180)
tan(x)(tan(x))
3sin(x)sin(x)/(3)
cos(x)sin(x)
tan(x)sec(x)^2
tan(x)^2-sec(x)^2
7+sin(x)csc(x)csc(x)+cot(x)^2
sin(90)+cos(-90)+sec(180)
csc(1)+csc(10)-csc(30)-csc(60)+csc(90)
sin(36000001)
csc(800)+(cot(720+x)-sec(4x-720))
sin(-x)+sin(x)
csc(-x)+csc(x)
4sin(x)-sin(x)+tan(x)-2tan(x)
cot(x)+10cot(x+90)+99cot(x+180)+.01cot(x-90)
tan(x)tan(x+180)
sec(x)sin(x+180)tan(x-270)cot(x-450)csc(x+90)
sin(x)/cot(x)+sin(x)/cot(x)
sin(x)csc(x)+tan(x)cot(x)+cos(x)sec(x)
cot(x)
9tan(x+90)+90tan(x+9)
cos(x-9999)+tan(x+99999)
2tan(x)tan(x)/2
tan(x)/tan(x-360)+cos(x+180)/cos(x)
csc(4x)sec(270-4x)
cot(91+x)tan(x-449)
csc(2x)(csc(2x)-sin(2x))
csc(x+1)^2-cot(x+1)cot(x+1)
cot(x)cot(x)+1
tan(x)^2-sec(x)sec(x)
tan(x)/cot(x)+csc(x)/csc(x)
cot(x)sin(x)/cos(x)
csc(x)tan(x)cos(x)
csc(x)cot(x)cos(x)
csc(x+90)csc(-x+270)-1
cot(x)/cot(x)+tan(x)/cot(x)+cot(x)cot(x)
sec(x)sec(x)sec(x)sec(x+90)sec(x-90)sec(x+180)
1-cos(x)cos(x+180)+sin(x)^2
sec(x)sec(x)sec(x)sec(x)/cos(x)+cot(x)
cot(x+1)csc(x-1)sec(x+1)tan(x-1)sin(x+1)cos(x-1)
sin(x)-cos(x)+tan(x)-sec(x)+cos(x)-csc(x)
tan(x+23515)-sec(-.27x-23456)
sec(-.9x)
-tan(-x)-csc(360-x)
cos(-x)sec(x)sin(x)csc(-x)
tan(-x)^2-sin(-x)/sin(x)
tan(x)tan(x)+1
csc(x)^2-1
cot(2x)cot(2x)-csc(2x)/sin(2x)
2sec(x)/(6cos(x))
sec(0)+tan(30)cos(60)-csc(90)sin(120)cot(150)+tan(180)csc(210)sin(240)-cos(270)sec(300)+cot(330)
tan(x-1234567)sec(4781053+x)^2
tan(-1234567x)sec(4781053x)^2
sin(x)^9+cos(x)^7+csc(x)^5-cot(x)^3
cos(-33x-7.7)
sec(-.1-x)+tan(-2x-77)
tan(x)+sin(x)-cos(x)+tan(x)-sin(x)
cos(x)-sec(x)/tan(x)+tan(x)/sin(x)
cot(x)-cos(x)/sin(x)
3cos(x)+2/sec(x)-10sin(x)/(2tan(x))
tan(x+3)^11+sin(x+3)^8-cos(x+3)^5+5
sec(x)+sec(x)
csc(.1x)csc(-.1x)csc(x)
cot(x-7)cot(x-7)cot(x+173)cot(x-7)cot(x+173)cot(x-367)
cos(.0001x+1234567)
cot(45)+tan(225)-sin(210)+cos(-60)
sin(12345)cos(23456)tan(34567)sec(45678)csc(56789)cot(67890)
cos(1234x)+cot(1234)+tan(1234x)+sec(1234)+csc(1234)+sin(1234x)
sec(x)sec(x)sec(x)sec(x)sec(x)sec(x)
csc(x)cot(x)sec(x)csc(x)tan(x)sec(x)cot(x)csc(x)cot(x)cos(x)cos(x)cot(x)sin(x)sin(x)cot(x)
csc(2553273)+cot(1507348)-sec(5518930)+csc(5215523)+tan(3471985)-sec(4985147)
sin(20x)+cos(20x)-tan(20x)+sin(20x)-csc(20x)+cot(20x)-tan(20x)+csc(20x)
cot(100000)+cot(100000x)+cot(100000x)+cot(100000)
csc(5x+777)sin(5x+777)
csc(4.5x)注意:这个场景完全是虚构的。在现实生活中,欺骗和帮助他人是错误的,永远不应该做。
发布于 2014-09-30 20:58:33
这肯定是最低效的解决方案之一,数学家可能会退缩,但我设法把一些实际上简化了大约一半的表达式,而没有任何硬编码:
from math import*
import exrex,re
d=lambda f:lambda x:f(x*pi/180)
sin,cos,tan=d(sin),d(cos),d(tan)
i=lambda f:lambda x:1/f(x)
sec,csc,cot=i(cos),i(sin),i(tan)
t=lambda s:[round(eval(re.sub('([\d\).])([\(a-z])','\\1*\\2',s).replace('^','**')+'+0')*1e9)for x in[1,44]]
def f(I):
try:return min((s for s in exrex.generate('(-?\d|\(1/\d\.\))?([+-]?(sin|cos|tan|cot|sec|csc)\(x\)(\^\d)?){0,4}')if s and t(s)==t(I)),key=len)
except:return I100个测试用例的输出应该如下(其中#标记未简化的表达式):
cos(x)
7+sin(x)
#
2+tan(x)^2cos(x)^2
(1/2.)cos(x)
sin(x)cos(x)^2
#
sin(x)^2tan(x)^7
1
#
#
sin(x)cos(x)
-1
-1
tan(x)^2
-cot(x)cos(x)
-cot(x)cos(x)^2
#
sin(x)
2sin(x)cos(x)
0
cos(x)^5
#
#
#
#
tan(x)^2
sin(x)^2
sin(x)cos(x)
tan(x)sec(x)^2
-1
7+cot(x)^2+csc(x)
0
#
#
#
0
0
3sin(x)-tan(x)
#
tan(x)^2
-tan(x)sec(x)
2sin(x)tan(x)
3
cot(x)
#
#
tan(x)^2
0
#
1
#
1
csc(x)^2
-1
sec(x)^2
1
1
cot(x)^2
-1-sec(x)^2
cot(x)^2+sec(x)^2
sec(x)^4csc(x)^2
2
cot(x)+sec(x)^5
#
sin(x)+tan(x)-csc(x)-sec(x)
#
#
tan(x)+csc(x)
-1
sec(x)^2
sec(x)^2
cot(x)^2
-1
(1/3.)sec(x)^2
#
#
#
#
#
#
2tan(x)-cos(x)
cos(x)-csc(x)+sec(x)
0
0
#
2sec(x)
#
#
#
3
#
#
sec(x)^6
cot(x)^4csc(x)
#
#
#
1
#这个想法很简单:
问题是有很多可能的表达式需要检查,所以我把它限制在一个子集上。
老实说,我没有足够的耐心去测试上面的黄金代码。(告诉我你是否认为这是作弊。)相反,我使用了一个更长但更有效的规则来生成一个更小的表达式子集,希望它仍然包含所有所需的简化。如果要尝试这样做,请将代码第二行至最后一行中的正则表达式替换为以下内容(并为几分钟的计算时间做好准备):
'0|((-1|\(1/[23]\.\)|[1237])[+-]?)?(sin\(x\)(\^2)?)?-?(tan\(x\)(\^[27])?)?(cot\(x\)(\^[24])?)?[+-]?(cos\(x\)(\^[25])?)?(sec\(x\)(\^[2456])?)?(csc\(x\)(\^2)?)?'该程序还包含了使Python理解数学表达式所需的大量开销。这涉及到
sin、cos和tan,使其以度来表示参数,而不是半径(我认为这就是OP的意图)sec、csc和cot,它们不是math包的一部分发布于 2014-09-30 07:58:12
就像给人们提供一些东西的基本解决方案一样:
l它将STDIN读入堆栈,其内容将在程序结束时打印出来。
我确信这是可以克服的,但我也确信,许多优化都不能在所获得的字符中实现。
发布于 2014-09-30 09:25:03
中间的对象可选择地包含基于输入长度和第一个字符的简化输出。否则,将不修改地返回输入。
s='sin(x)';(p=prompt)({
352:s+"^2cos(x)^3+2",
307:"cos(x)/2",
348:s+"8(1+cos(x)^2)",
"81s":s+"^2tan(x)^7",
"55s":"-sin(0.2-0.1x)^5",
"39s":"2sec(2x+1)^2",
"35c":"cos(4x)",
"36-":0,
173:s+"^2",
"25s":0,
294:s+"3-tan(x)",
"27s":s+"2/cot(x)",
"40c":"1+tan(x)^2+cot(x)^2",
"48c":1,
"41s":s+"+tan(x)-csc(x)-sec(x)",
"26c":-1,
"24t":"1+tan(x)^2",
"96s":"2.5+1/cos(30)",
"34t":"2tan(x)-cos(x)",
353:0,
"36s":"6sec(x)",
"90s":"cot(x)^4csc(x)"
}[(i=p()).length+i[0]]||i)空格仅用于装饰,不包括在字符计数中。
遗憾的是,添加所有这些解决方案只为我节省了几个字符,但总比什么都没有好。简化是通过wolframalpha完成的--我相信它。
https://codegolf.stackexchange.com/questions/38341
复制相似问题