Haskell - How to transform maximum (xs ++ map (x+) xs) to max (maximum xs) (x + maximum xs)
Date : March 29 2020, 07:55 AM
hope this fix your issue This can be seen by induction. Suppose, xs == []. Both expressions are true, since both yield error. maximum([y]++map(x+)[y]) == -- by definition of map
== maximum([y]++[x+y])
-- by definition of ++
== maximum([y,x+y])
-- by definition of maximum
== foldr1 max [y,x+y]
-- by definition of foldr1
== max y (foldr1 max [x+y])
-- by definition of foldr1
== max y (x+y)
-- by definition of foldr1 and maximum [y]
== max (maximum [y]) (x+maximum [y])
maximum ([]++(y:ys)) == maximum (y:ys)
-- by definition of foldr1 and maximum
== max y (maximum ys)
== max y (maximum ([]++ys))
maximum(x:xx++(y:ys)) == maximum (x:(xx++(y:ys)))
-- by definition of foldr1 and maximum
== max x (maximum (xx++(y:ys)))
-- by induction
== max x (max y (maximum (xx++ys)))
-- by commutativity of max, max a (max b c) == max b (max a c)
== max y (max x (maximum (xx++ys)))
-- by definition of foldr1 and maximum
== max y (maximum (x:(xx++ys)))
-- by definition of ++
== max y (maximum ((x:xx) ++ ys))
maximum (y:ys ++ map(x+)(y:ys)) ==
-- by definition of map
== maximum(y:ys ++ (x+y):map(x+)ys)
-- by definition of foldr1 and maximum
== max y (maximum(ys ++ (x+y):map(x+)ys)
-- by commutativity of maximum
== max y (max (x+y) (maximum (ys++map(x+)ys)))
-- by induction, (maximum (ys++map(x+)ys)) == max (maximum ys) (x+maximum ys))
== max y (max (x+y)
(max (maximum ys) (x+maximum ys)))
-- by commutativity of max (ie max a (max b c) == max b (max a c))
== max y (max (maximum ys)
(max (x+y) (x+maximum ys)))
-- by associativity of max (is max a (max b c) == max (max a b) c)
== max (max y (maximum ys))
(max (x+y) (x+maximum ys)))
-- by definition of max, max (x+y) (x+z) == x+(max y z)
== max (max y (maximum ys))
(x + max y (maximum ys)))
-- by definition of foldr1 and maximum
== max (maximum (y:ys)) (x + maximum (y:ys))
data Nat = Z | S Nat -- zero and any successor of a natural number
(+) :: Nat -> Nat -> Nat -- addition is...
Z + x = x -- adding zero is neutral
(S x) + y = S (x + y) -- recursive definition of (1+x)+y = 1+(x+y)
-- here unwittingly we introduced associativity of addition:
-- (x+y)+z=x+(y+z)
-- so, let's see the simplest case:
-- x == Z
-- (Z+y)+z == -- by definition, Z+y=y -- see the first line of (+)
-- == y+z
-- == Z+(y+z) -- by definition, Z+(y+z)=(y+z)
--
-- ok, now try x == S m
-- (S m + y) + z == -- by definition, (S m)+y=S(m+y) -- see the second line of(+)
-- == S (m+y) + z
-- == S ((m+y)+z) -- see the second line of (+)
-- - S (m+y) + z = S ((m+y)+z)
-- == S (m+(y+z)) -- by induction, the simpler
-- case of (m+y)+z=m+(y+z)
-- is known to be true
-- == (S m)+(y+z) -- by definition, see second line of (+)
-- proven
max :: Nat -> Nat -> Nat
max Z y = y -- we know Z is not the max
max x Z = x -- and the other way around, too
-- this inadvertently introduced commutativity of max already
max (S x) (S y) = S (max x y) -- this inadvertently introduces the law
-- that max (x+y) (x+z) == x + (max y z)
max (Z+y) (Z+z) == -- by definition of (+)
== max y z
== Z + (max y z) -- by definition of (+)
max ((S m) + y) ((S m)+z) == -- by definition of (+)
== max (S(m+y)) (S(m+z))
-- by definition of max
== S (max (m+y) (m+z))
-- by induction
== S (m+(max y z))
-- by definition of (+)
== (S m)+(max y z)
|
Distance along x-axis between Global Maximum of a Wave Pattern and next Consecutive Maximum/Minimum
Tag : matlab , By : user157064
Date : March 29 2020, 07:55 AM
will be helpful for those in need Simply search for the first minimum that is after your global maximum, or the end of the data set if the maximum is last. %% create the data set
x = rand(150,1);
idx = 55:85;
xx = x(idx);
xx = xx*max(x(:))/max(xx(:))*1.2; %% create a peak that will be in the middle of the data
x(idx) = xx;
x = smooth(x);
x = smooth(x);
%% find the next minimum
[pospks,locks] = findpeaks(x); %find all the maximums
[~,locksN] = findpeaks(-x); %find only the location of all the minimums
[~,maxIdx] = max(pospks); %find the location of the (first)global maximum
maxIdx = locks(maxIdx);
minIdxs = locksN(locksN > maxIdx); %find the indexes of all the minimums that are after the global maximum
if (isempty(minIdxs))
minIdx = length(x); % take the last element of the dataset
else
minIdx = minIdxs(1); % take the first min after the global max
end
plot(1:length(x),x,'green');
hold on;
plot(maxIdx,x(maxIdx),'bo');
plot(minIdx,x(minIdx),'r*');
hold off;
|
Most efficient method to find maximum element and list of maximum elements from a 2-d array?
Date : March 29 2020, 07:55 AM
may help you . Suppose I have a list arr=[[1,2,3],[4,5,6],[7,8,9]] here maximum element is 9 and maximum elements from 1 d list is [3,6,9]. , If using numpy >>>import numpy as np
>>>array=np.random.rand(3,3)
>>>print(array)
>>>print(array.max(axis=1))
>>>[[ 0.76562624 0.45225107 0.74276688]
[ 0.84390255 0.03384166 0.40036534]
[ 0.00371805 0.47996941 0.15593055]]
>>>[ 0.76562624 0.84390255 0.47996941]
>>>arr=[[1,2,3],[4,5,6],[7,8,9]]
>>>print(list(map(max,arr)))
>>>[3,6,9]
>>>print(max(map(max,arr)))
>>>9
>>>print(array.max())
|
filter data by maximum group sample size with duplicate maximum values
Tag : r , By : hlpimfalling
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , You are halfway there. You need to group by just Species, sort according to what you want to keep, and take the top row from each species. iris %>%
group_by(Species, Year) %>%
summarise(N = n()) %>%
group_by(Species) %>%
arrange(desc(N), Year) %>%
slice(1)
# # A tibble: 3 x 3
# # Groups: Species [3]
# Species Year N
# <fctr> <chr> <int>
# 1 setosa 2007 25
# 2 versicolor 2008 40
# 3 virginica 2007 40
|
Determining the maximum font size for a multi-line UILabel with maximum width
Date : March 29 2020, 07:55 AM
wish help you to fix your issue If I got your code right, you're trying to decrease font size until it will fit the UILabel size. So you the size of the font would be as large as possible. The main problem is in your while loop. On each step, you set up new font and call sizeToFit what makes you label frame change to fit the font size. After that your isTruncated will return false. testLabel.numberOfLines = 3
testLabel.text = message
while font > 13 {
testLabel.font = UIFont.systemFont(ofSize: font, weight: .regular)
testLabel.frame.size = CGSize(width: maximumContentWidth,
height: maximumContentHeight)
if !testLabel.isTruncated {
break
}
font -= 0.1
}
extension UILabel {
func getLinesCount() -> Int {
let text = self.text ?? ""
let rectWidth = self.frame.width
let myFont = CTFontCreateWithName(font.fontName as CFString, font.pointSize, nil)
let attStr = NSMutableAttributedString(string: text)
let range = NSRange(location: 0, length: attStr.length)
attStr.addAttribute(kCTFontAttributeName as NSAttributedStringKey, value: myFont, range: range)
let frameSetter = CTFramesetterCreateWithAttributedString(attStr as CFAttributedString)
let path: CGMutablePath = CGMutablePath()
let rect = CGRect(x: 0, y: 0, width: rectWidth, height: CGFloat.greatestFiniteMagnitude)
path.addRect(rect, transform: .identity)
let frame: CTFrame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, nil)
guard let lines = CTFrameGetLines(frame) as? [Any] else { return 0 }
return lines.count
}
}
|