I have the following code
def f(x, y, ro):
return x*y*ro
def bounds_y():
return [0, 0.5]
def bounds_x(y):
return [0, 1-2*y]
scipy.integrate.nquad(f, [bounds_x, bounds_y, [0.5] ], args= [0.5])
Without the args, the code works. But when I add the args, I am getting the following error
bounds_y() takes 0 positional arguments but 1 was given
Why this is the case and how to resolve it?
Thanks!
Related
def f(x: Int): Boolean = (x >= 0 && x < 4)
List(1, 3, 5).map(f) // List(true, true, false)
f // does not compile
Why can f be used where a function value is expected, even if it is not a function value itself?
What is happening?
In places where a function type is expected, f is converted to an anonymous function (x: Int) => f(x).
def f(x: Int): Boolean = (x >= 0 && x < 4)
// f // f itself is not a function value
f(_) // f(_) is an anonymous function
List(1, 3, 5).map(f) // f is converted to f(_) in places where a function type is expected
List(1, 3, 5).map(f(_)) // equivalent to last line
Why is f not a function value in the first place?
Because the parameterless function f was not defined. A curried (parameterless) function value would work:
val g = (x: Int) => (x >= 0 && x < 4)
g
Why is f accepted as a function value?
map expects a function type and because curried and uncurried versions of f f and g both do the same, the automatic conversion makes sense.
another advantage is that map(f) has a cleaner look than map(f(_)).
a disadvantage of all the automatic and syntactic sugar stuff that is done for you, is that it can be confusing
Hi I'm newbie in scala.
when I write the below code, Even I didn't declared variable y, but it is allowed.
object exercise {
def fixedPoint(f: Double => Double)(firstGuess: Double) = {
//some code
}
def sqrt(x: Double) = fixedPoint(y => x / y)(1) //No error. weird...
}
I don't understand how does it works?
I didn't declared variable y
Actually, you did.
This ...
y => ...
... translates into, "Here's a function that takes a single argument. I'm going to call that argument y."
When you declare f: Double => Double, it means that function f will take Double as input parameter and will return Double as output. So, when you pass y => x / y function, y here is of type Double and x/y(output) is of type double.
It is not giving any compiler error because compiler can infer the type based on type of function fixedPoint expects.
Suggest you have a look for How to use function literals (anonymous functions) in Scala.
In the post, it use a simple example (i: Int) => i % 2 == 0 to act as a function literals, and pass it to the function filter. And see follows:
Because the Scala compiler can infer from the expression that i is an Int, the Int declaration can be dropped off: val evens = x.filter(i => i % 2 == 0)
Then, this is exactly the situation in your post for y => x / y, the Double was dropped off in your code as scala compiler can infer from the expression.
Before I answer this question let me modify your code as:
object exercise {
def fixedPoint(f: Double => Double)(firstGuess: Double) = {
f(firstGuess) //call the function in f with the curried parameter
}
def sqrt(x: Double) = fixedPoint(y => x / y)(1) //No error. weird...
}
I have added a line to call function f (which takes Double and returns Double) with firstGuess parameter.
f(firstGuess) //call the function in f with the curried parameter
Let's see what what sqrt function is all about
def sqrt(x: Double) = fixedPoint(y => x / y)(1)
It takes a x and invokes fixedPoint with y => x/y and 1
now your y is firstGuess parameter (as per modified code) and x is the sqrt parameter.
I expect the following code output with Seq(0), instead it returns a function ?
# Seq(0).orElse(Seq(1))
res2: PartialFunction[Int, Int] = <function1>
I suspected at first that via syntax sugar it orElse on apply function, but it didn't since by trying:
# Seq(0).apply.orElse(Seq(1))
cmd3.sc:1: missing argument list for method apply in trait SeqLike
....(omit)
I checked in IntellJ that there's no implicit conversion.
What happens?
EDIT:
what I wish is:
Seq.empty.orElse(Seq(1)) == Seq(1)
Seq(0).orElse(Seq(1)) == Seq(0)
thanks #AndreyTyukin answer.
In one line, orElse has different semantic in different type , now Seq inherits PartialFunction not Option, so does the orElse behavior.
The Seq(0) is treated as a PartialFunction that is defined only at index 0, and produces as result the constant value 0 if it is given the only valid input 0.
When you invoke orElse with Seq(1), a new partial function is constructed, that first tries to apply Seq(0), and if it finds nothing in the domain of definition of Seq(0), it falls back to Seq(1). Since the domain of Seq(1) is the same as the domain of Seq(0) (namely just the {0}), the orElse does essentially nothing in this case, and returns a partial function equivalent to Seq(0).
So, the result is again a partial function defined at 0 that gives 0 if it is passed the only valid input 0.
Here is a non-degenerate example with sequences of different length, which hopefully makes it easier to understand what the orElse method is for:
val f = Seq(1,2,3).orElse(Seq(10, 20, 30, 40, 50))
is a partial function:
f: PartialFunction[Int,Int] = <function1>
Here is how it maps values 0 to 4:
0 to 4 map f
// Output: Vector(1, 2, 3, 40, 50)
That is, it uses first three values from the first sequence, and falls back to the second sequence passed to orElse for inputs 3 and 4.
This also works with arbitrary partial functions, not only sequences:
scala> val g = Seq(42,43,44).orElse[Int, Int]{ case n => n * n }
g: PartialFunction[Int,Int] = <function1>
scala> 0 to 10 map g
res7 = Vector(42, 43, 44, 9, 16, 25, 36, 49, 64, 81, 100)
If you wanted to select between two sequences without treating them as partial functions, you might consider using
Option(Seq(0)).getOrElse(Seq(1))
This will return Seq(0), if this is what you wanted.
given the following input:
scala> val as = List(Array(1,2,3), Array(10,20,30), Array(100,200,300))
as: List[Array[Int]] = List(Array(1, 2, 3), Array(10, 20, 30), Array(100, 200, 300))
I am wondering why this works:
EXAMPLE 1
scala> as.reduce((x,y) => x)
res65: Array[Int] = Array(1, 2, 3)
But this seemly identical thing does not work:
EXAMPLE 2
scala> as.reduce{case(x,y) => x}
<console>:13: error: missing parameter type for expanded function
The argument types of an anonymous function must be fully known. (SLS 8.5)
Expected type was: (?, ?) => ?
as.reduce{case(x,y) => x}
Can anyone explain why the 1st example works but not the 2nd example?
As the error message said, you need to specify type Array[Int]. This one will work:
as.reduce[Array[Int]]{ case (x,y) => x}
There is a good explanation here.
In Example 1, the argument to reduce is a Function2, taking two inputs (x and y) and returning x.
In Example 2, the argument to reduce is a Function1, taking one input, which happens to be a Tuple2, and returning the first element in the tuple.
The reduce method requires a Function2 as its argument, and so that's why Example 1 works, but Example 2 doesn't.
I am unsure how to first of all correct this problem of missing required positional arguments, but after this I am trying to set changeable default values for n, s, x and y. I apologize if this question seems stupid, but I am new and appreciate any and all help.
import math
class RegularPolygon:
def __init__(self, n, s, x, y):
self.n = 3
self.side = 1
self.x = 0
self.y = 0
def getN(self, n):
self.n = n
return self.n
def getSide(self, s):
self.side = s
return self.side
def getX(self, x):
self.x = x
return self.x
def getY(self, y):
self.y = y
return self.y
def getPerimeter(self, n , s):
self.perimeter = n * s
return self.perimeter
def getArea(self, n, s):
self.area = (n * math.pow(s, 2)) / ( 4 * math.tan(math.pi / n))
return self.area
The error:
Traceback (most recent call last):
File "C:\Pythonstuff\Ch.7\chapter07_unittests.py", line 7, in setUp
self.poly1 = RegularPolygon()
TypeError: __init__() missing 4 required positional arguments: 'n', 's', 'x', and 'y'
You need to pass values when creating a RegularPolygon object:
c = RegularPolygon(100, 100, 100, 100)
You are now probably writing:
c = RegularPolygon()
In this class there are four positional arguments and you're not passing any now. As none of these parameters have a default value they're all required so you need to specify exactly four values in this case.